loop for character variable names in r

后端 未结 2 1213
余生分开走
余生分开走 2021-01-26 04:21

I have a list of variables, like a1, a2, a3. Then I want to do a loop for these variable, for instance, assign values. I tried paste, which just gave character like \"a1\", that

2条回答
  •  一个人的身影
    2021-01-26 05:13

    We can use mget to get the values in a list

    lst <- mget(paste0("a", 1:3))
    

    and then loop through the list and apply the function

    lapply(lst, yourFunction)
    

提交回复
热议问题