gpt4 book ai didi

r - 是什么导致 filter() 和 across() 函数出现 'object not found' 错误?

转载 作者:行者123 更新时间:2023-12-05 01:58:36 24 4
gpt4 key购买 nike

此函数从我的数据集中筛选/选择一个或多个变量,并将其写入新的 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=''))


}

这里是函数调用。我正在尝试获取列“rid”和“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 正确并出现在数据中。我也试过只用一列运行它,删除,并得到相同的“找不到对象”错误。

最佳答案

如果您将多个值传递给across(),您需要将它们收集在第一个参数中,否则它们将散布到across() 的其他参数中.尝试

filter(df, across(c(...), ~ !is.na(.x)) 

否则,除第一个值之外的每个值都将作为参数传递给您在 across()

中指定的函数

关于r - 是什么导致 filter() 和 across() 函数出现 'object not found' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68489883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com