基于数据框名称中的常见模式的 rbind 数据框 [英] rbind data frames based on a common pattern in data frame name

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

问题描述

假设我有多个数据帧,它们都具有相同的向量名称,并且我想将所有具有共同模式的数据帧 cbind.所以对于这 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 列的数据框,有效地 cbinding 整个事情而不是 rbinding.

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

<小时>

或者使用 rbind 来自 base R

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

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

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