r - 使用 dplyr 从其他数据帧创建一个新数据帧
问题描述
我有这 3 个数据框:
df.1<-as.data.frame(matrix(rnorm(10),250,149))
df.2<-as.data.frame(matrix(rnorm(10),250,149))
df.3<-as.data.frame(matrix(rnorm(10),250,149))
我想用这些条件创建另一个数据框:
the columns 1,4,7,10,...445 of df.4... will be the columns from df.1
the columns 2,5,8,11,...446 of df.4... will be the columns from df.2
the columns 3,6,9,12,...447 of df.4... will be the columns from df.3
最后,我的 df.4 将有3*149
列。
如何使用dplyr
包及其功能做到这一点?
最重要的是,我怎样才能维护我的列名???
解决方案
为此,我们将数据帧与bind_cols()
然后重新排序组合select()
如下:
library (dplyr)
df.4 <- bind_cols(df.1, df.2, df.3)
# create the column order
order <- c()
for(i in 1:149){
temp <- c(i, i+149, i+298)
order <- c(order, temp)
}
df.4 <- df.4 %>% select(order)
这里使用的循环是循环数字 1:149 并且无论数据大小如何都不会很慢
推荐阅读
- r - axis.text 不改变角度
- jenkins - Jenkinsfile 管道中的“如果”条件(Windows)
- azure - 由于文件路径,Azure 函数无法在本地运行?
- node.js - Kubernetes 中的 redis 和 nodejs 出错
- javascript - Javascript/html 未定义的输入
- python-3.x - 如何检索特殊条目。和..在一个文件夹中
- javascript - 下面这段代码的未定义输出有问题
- rust - 在结构中使用 trait 对象而不需要类型参数
- python - OOP继承,如何在python中获取父类型的变量?
- excel - 跟踪命名范围内注释中的单元格更改