Assign names to variable using regular expression in R

后端 未结 1 918
一个人的身影
一个人的身影 2021-01-16 00:57

So I have a bunch of variables in my workspace. I want to assign a subset of them to a new variable, so I can easily run functions on this subset:

workspace:

相关标签:
1条回答
  • 2021-01-16 01:34

    ls accepts a pattern argument:

    group10 <- group40 <- location40 <- test <- NA
    mysub <- ls(pattern="^group[0-9]+")
    mysub
    #[1] "group10" "group40"
    

    You can use lapply to loop over the list of variable names and get their values

    groupList <- lapply(mysub, get)
    

    or, in one line

    groupList <- lapply(ls(pattern="^group[0-9]+"), get)
    
    0 讨论(0)
提交回复
热议问题