是什么导致使用 cross() 函数的 filter() 中出现“找不到对象"错误? [英] What is causing 'object not found' error in filter() with the across() function?

查看:44
本文介绍了是什么导致使用 cross() 函数的 filter() 中出现“找不到对象"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数从我的数据集中过滤/选择一个或多个变量并将其写入新的 CSV 文件.调用该函数时出现找不到对象"错误.这是函数:

This function filters/selects one or more variables from my dataset and writes it to a new CSV file. I'm getting an 'object not found' error when I call the function. Here is the function:

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

And here is the function call. I'm trying to get columns "rid" and "cintid."

extract_ids(filename = "farmers.csv",
            opp = "farmers",
            rid, cintid)

当我运行这个时,我收到以下错误:

When I run this, I get the below error:

 Error: Problem with `filter()` input `..1`.
ℹ Input `..1` is `across(..., ~!is.na(.x)) & gc == 1`.
x object 'cintid' not found

cintid 列正确,出现在数据中.我也试过只用一列运行它,摆脱,并得到相同的找不到对象"错误.

The column cintid is correct and appears in the data. I've also tried running it with just one column, rid, and get the same 'object not found' error.

推荐答案

抱歉在我的之前的建议中省略了这个你.不幸的是,你的原始问题在我之前就被关闭了将其发布为答案:

Sorry for omitting this in my previous suggestion to you. Unfortunately, your original question was closed before I could post it as an answer:

如果你想让你的函数类似于 dplyr,这里有一些您可以进行修改.将您的函数头写为function(filename, opp, ...) 逐字逐句.然后,替换 !is.na(ID)across(..., ~ !is.na(.x)) 逐字逐句.现在,你可以打电话extract_ids() 并且,就像使用任何 dplyr 动词一样,您可以指定要过滤掉 NA 的任何列选择:extract_ids(filename = "farmers.csv", opp = "farmers",rid, another_column_you_want_without_NAs).

If you want your function to resemble dplyr, here's a few modifications you can make. Write your function header as function(filename, opp, ...) verbatim. Then, replace !is.na(ID) with across(..., ~ !is.na(.x)) verbatim. Now, you can call extract_ids() and, just as you would with any dplyr verb, you can specify any selection of columns you want to filter out NAs: extract_ids(filename = "farmers.csv", opp = "farmers", rid, another_column_you_want_without_NAs).

未找到对象

正如 MrFlick他们的评论,你应该用 c() 包裹 ...,所以你在 ... 中传递的一切都是解释为 across() 的第一个参数:单个 来自 df 的列的 tidy-selection:

Object Not Found

As MrFlick rightly suggests in their comment, you should wrap ... with c(), so everything you pass in ... is interpreted as the first argument to across(): a single tidy-selection of columns from df:

extract_ids <-  function(filename, opp, ...) {
  # ...

  # Filter and select
  df_id <- df %>%
    # This format is preferred for dplyr workflows with pipes (%>%).
    filter(across(c(...), ~ !is.na(.x)) & gc == 1) %>%
    select(...)

  # ...
}

如果没有这种预防措施,R 会将 ridcintid 解释为 across() 的多个参数,而不是简单的由第一个命名的列参数(tidy-selection).

Without this precaution, R interprets rid and cintid as multiple arguments to across(), rather than as simply columns named by the first argument (the tidy-selection).

要在文件路径中获取这些变量名称,请使用

To get those variable names within your filepath, use

extract_ids <-  function(filename, opp, ...) {
  # ...
  
  # Expand the '...' into a list of given variable names, which will get pasted.
  path <- c("/Users/stephenpoole/Downloads/", opp, "_", match.call(expand.dots = FALSE)$`...`, ".csv")

  # ...
}

尽管您可能想考虑替换 match.call(expand.dots = FALSE)$`...`,它目前将变量名称混在一起:

though you might want to consider replacing match.call(expand.dots = FALSE)$`...`, which currently mushes together the variable names:

"/Users/stephenpoole/Downloads/farmers_ridcintid.csv"

在完全相同的地方,您可以使用表达式 paste(match.call(expand.dots = FALSE)$`...`, collapse = "-"),它将使用 -

In exactly the same place, you might use the expression paste(match.call(expand.dots = FALSE)$`...`, collapse = "-"), which will separate those variable names using -

"/Users/stephenpoole/Downloads/farmers_rid-cintid.csv"

或您选择的任何其他提供有效文件名的分隔符.

or any other separator of your choice that gives a valid filename.

这篇关于是什么导致使用 cross() 函数的 filter() 中出现“找不到对象"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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