【发布时间】:2021-07-22 18:27:21
【问题描述】:
此函数从我的数据集中过滤/选择一个或多个变量并将其写入新的 CSV 文件。调用该函数时出现“找不到对象”错误。这是函数:
extract_ids <- function(filename, opp, ...) {
#Read in data
df <- read_csv(filename)
#Remove rows 2,3
df <- df[-c(1,2),]
#Filter and select
df_id <- filter(df, across(..., ~ !is.na(.x)) & gc == 1) %>%
select(...) #not sure if my use of ... here is correct
#String together variables for export file path
path <- c("/Users/stephenpoole/Downloads/",opp,"_",...,".csv") #not sure if ... here is correct
#Export the file
write_csv(df_id, paste(path,collapse=''))
}
这里是函数调用。我试图让列“摆脱”和“cintid”。
extract_ids(filename = "farmers.csv",
opp = "farmers",
rid, cintid)
当我运行它时,我收到以下错误:
Error: Problem with `filter()` input `..1`.
ℹ Input `..1` is `across(..., ~!is.na(.x)) & gc == 1`.
x object 'cintid' not found
cintid 列正确并出现在数据中。我也试过只用一列运行它,去掉,得到同样的“找不到对象”错误。
【问题讨论】:
-
如果你要传递多个值,它应该像
filter(df, across(c(...), ~ !is.na(.x))这样这些列都在第一个参数中。如果您包含一个简单的reproducible example 以及可用于测试和验证可能解决方案的示例输入,则更容易为您提供帮助。 -
@StephenPoole 我刚刚更新了我的答案,解决了您关于文件路径中的变量名称的问题。