bind_rows()错误:通过读取函数? [英] bind_rows() error: by reading in a function?

查看:71
本文介绍了bind_rows()错误:通过读取函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码块在下面运行,并按预期生成 df_all ,但是当我取消注释顶部的单个函数时(甚至不在此处应用它,但我确实需要其他东西) ),然后重新运行同一块,我得到: bind_rows_(x,.id)中的错误:参数1必须是数据帧或命名的原子向量,而不是函数

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


推荐答案

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

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.*")

这篇关于bind_rows()错误:通过读取函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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