bind_rows()错误:通过读取函数?

时间:2018-08-15 06:01:49

标签: r data.table

此块在下面运行,并按预期产生df_all,但是当我取消注释顶部的单个函数(甚至不在此处应用但我确实需要其他东西)并重新运行同一块时,我得到了:Error in bind_rows_(x, .id): Argument 1 must be a data frame or a named atomic vector, not a function

library(data.table)

#  addxtoy_newy_csv <- function(df) {
#    zdf1 <- df %>% filter(Variable == "s44") 
#    setDT(df)
#    setDT(zdf1)
#    df[zdf1, Value := Value + i.Value, on=.(tstep, variable, Scenario)]
#    setDF(df)  
#}

tstep <- rep(c("a", "b", "c", "d", "e"), 5)
Variable <- c(rep(c("v"), 5), rep(c("w"), 5), rep(c("x"), 5), rep(c("y"), 5), rep(c("x"), 5))  
Value <- c(1,2,3,4,5,10,11,12,13,14,33,22,44,57,5,3,2,1,2,3,34,24,11,11,7)
Scenario <- c(rep(c("i"), 20), rep(c("j"), 5) ) 
df1 <- data.frame(tstep, Variable, Value, Scenario)

tstep <- c("a", "b", "c", "d", "e")
Variable <- rep(c("x"), 5) 
Value <- c(100, 34, 100,22, 100)
Scenario <- c(rep(c("i"), 5))
df2<- data.frame(tstep, Variable, Value, Scenario)

setDT(df1)
setDT(df2)
df1[df2, Value := Value + i.Value, on=.(tstep, Variable, Scenario)]
setDF(df1)

df_all <- mget(ls(pattern="df*")) %>% bind_rows() 

1 个答案:

答案 0 :(得分:5)

您在ls()中使用的模式将匹配名称中带有“ d”的任何对象,因此addxtoy_newy_csv会包含在对象名称列表中。模式中的f*表示您当前正在搜索“ d,后跟零个或多个f”。我认为使用更安全的模式是^df.*,以匹配以“ df”开头的对象:

df1 = data.frame(x = 1:3)
df2 = data.frame(x = 4:6)

adder = function(x) x + 1

ls(pattern = "df*")
ls(pattern = "^df.*")