【问题标题】:Rbind dataframes using wildcard使用通配符 Rbind 数据帧
【发布时间】:2014-02-03 15:10:33
【问题描述】:

假设我有三个名为 A1-pre、B3-pos、B4-pre 的数据帧,我想合并这些数据帧。这些列具有相同的名称,因此我可以使用 rbind。

newdf <- rbind(A1-pre, B3-pos, B4-pre)  #this would work

但是,我不想自己手动输入数据框的所有名称,我宁愿为此使用通配符,比如

newdf <- rbind(grep(-)) #but this does not work

知道我该怎么做吗?或者更好的是,匹配任何名为“pre”或“pos”的数据框并将它们全部 rbind。

【问题讨论】:

  • 你真的用-命名了你的data.frames?考虑使用 ls()do.call()get()

标签: r wildcard rbind


【解决方案1】:

您可以使用get()ls()

'A1-pre' <- matrix(rnorm(100), 5) 
'B3-pos' <- matrix(rnorm(100), 5)
'B4-pre' <- matrix(rnorm(100), 5)
'C5-not' <- matrix(rnorm(100), 5)

names <- grep('pre|pos$', ls(), value=T)

newDF <- mapply(get, grep('pre|pos$', ls(), value=T))

【讨论】:

  • do.call(rbind, lapply(ls(pattern = "-pre"), get)) 等等,正如我在评论中提到的那样。
猜你喜欢
  • 2013-12-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2018-06-04
相关资源
最近更新 更多