R - 在具有相似名称的多个变量上使用 rbind [英] R - use rbind on multiple variables with similar names

查看:39
本文介绍了R - 在具有相似名称的多个变量上使用 rbind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建了许多变量:

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

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

我现在需要对变量使用 rbind 来组合它们.我试过这样的事情无济于事:

I now need to use rbind on the variables to combine them. I tried something like this to no avail:

rbind(assign(paste0("variable", 1:10)))

有什么建议吗?

推荐答案

那是处理相关项目的错误方式.最好使用列表或数据框,但您可能会在适当的时候找出原因.目前:

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) )

或者:

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

这篇关于R - 在具有相似名称的多个变量上使用 rbind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆