基于数据帧名称中的通用模式来绑定数据帧 [英] rbind data frames based on a common pattern in data frame name

查看:54
本文介绍了基于数据帧名称中的通用模式来绑定数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有多个数据框,它们的向量名称都相同,我想将所有具有逗号模式的数据框绑定在一起.因此,对于这3个数据帧:

Say I have multiple data frames which all have identical vector names and I'd like to cbind all which have a commmon pattern. So for these 3 data frames:

df.1 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)),
                   speed=runif(10))
df.2 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)),
                   speed=runif(10))
df.3 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)),
                   speed = runif(10))

我想rbind使用通用模式"df.*"的所有内容

I would like to rbind everything with the common pattern "df.*"

我尝试创建列表,然后使用以下方法从中创建数据框:

I have tried creating a list and then creating a data-frame from this using:

temp <- lapply(ls(pattern = "df.*"), get) 
temp2<- as.data.frame(temp)

但是,这只会产生一个6列的数据帧,有效地绑定了整个对象,而不是绑定.

However this only produces a data frame of 6 columns effectively cbinding the whole thing rather than rbinding.

推荐答案

我们可以将lsmget

library(data.table)
rbindlist(mget(ls(pattern = "^df\\.\\d+")))


或使用dplyr

library(dplyr)
mget(ls(pattern="^df\\.\\d+")) %>%
              bind_rows()


或使用base R

do.call(rbind, mget(ls(pattern="^df\\.\\d+")))

这篇关于基于数据帧名称中的通用模式来绑定数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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