dbo
dbo

Reputation: 1234

bind_rows() error: by reading in a function?

This block runs below, and produces df_all as intended, but when I uncomment the single function at the top (not even apply it here but I do need for other things) and rerun the same block, I get: 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() 

Upvotes: 0

Views: 160

Answers (1)

Marius
Marius

Reputation: 60090

The pattern you use in ls() will match any object with a "d" in its name, so addxtoy_newy_csv gets included in the list of object names. The f* in your pattern means you currently search for "d, followed by zero or more f's". I think a safer pattern to use would be ^df.*, to match objects that start with "df":

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

adder = function(x) x + 1

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

Upvotes: 5

Related Questions