我如何在循环中绑定()组数据帧以生成多个数据集 [英] How can I cbind() sets of data frames in a loop to generate multiple data sets
本文介绍了我如何在循环中绑定()组数据帧以生成多个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以说4个数据集 f1,f2,i1,i2
。我想 cbind()
f1
与 i1
和 f2
和 i2
。
I have let say 4 data sets f1,f2,i1,i2
. I want to cbind()
f1
with i1
and f2
with i2
.
我可以使用
v1<-cbind(f1,i1)
v2<-cbind(f2,i2)
,但我想以某种循环的方式进行。
but I want to do this in some sort of loop.
I知道这个问题很基本。但是经过大量搜索之后,我仍然找不到解决方案。
I know the question is very basic. But after lots of searching I am still unable to find a solution for this.
推荐答案
我们可以使用将
映射到 cbind
两个数据集的对应列
We can use Map
to cbind
the corresponding columns of both datasets
lst <- setNames(Map(cbind, mget(ls(pattern = "^f\\d+")),
mget(ls(pattern = "^i\\d+"))), paste0("v", seq_along(f1)))
创建列表
数据集。
f1 <- data.frame(col1 = 1:5, col2 = 6:10)
f2 <- data.frame(col1 = 1:10, col2 = 11:20)
i1 <- data.frame(col3 = 11:15, col4 = 16:20)
i2 <- data.frame(col3 = 21:30, col4 = 31:40)
这篇关于我如何在循环中绑定()组数据帧以生成多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文