R - use rbind on multiple variables with similar names

前端 未结 2 1993
离开以前
离开以前 2020-11-28 13:06

I have many variables that I have created using code like this:

for (i in 1:10) {
    assign(paste0(\"variable\", i), i )}

I now need to us

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 13:57

    That is the wrong way to handle related items. Better to use a list or dataframe, but you will probably find out why in due course. For now:

    do.matrix <- do.call(rbind, lapply( ls(patt="variable"), get) )
    

    Or:

    do.matrix <- do.call(rbind, lapply( paste0("variable", 1:10) , get) )
    

提交回复
热议问题