Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于在`dplyr::cross()`中使用`where()`的警告以及使用时的错误_R_Dplyr - Fatal编程技术网

关于在`dplyr::cross()`中使用`where()`的警告以及使用时的错误

关于在`dplyr::cross()`中使用`where()`的警告以及使用时的错误,r,dplyr,R,Dplyr,我是一个初学者,我正在尝试清理Excel电子表格中的数据。我读过关于dplyr::cross()的文章,所以我尝试在mutate管道中使用它 我需要将一些被错误导入的列转换为字符——这些是指整数,但我认为可能偶尔会有输入错误,例如额外的空格会让人混淆readxl::readxlsx() 我正在尝试运行以下代码,该代码确实有效,但会从dplyr生成警告: library(dplyr, warn.conflicts = FALSE) # Copy built-in DF my_iris <

我是一个初学者,我正在尝试清理Excel电子表格中的数据。我读过关于
dplyr::cross()
的文章,所以我尝试在
mutate
管道中使用它

我需要将一些被错误导入的列转换为
字符
——这些是指整数,但我认为可能偶尔会有输入错误,例如额外的空格会让人混淆
readxl::readxlsx()

我正在尝试运行以下代码,该代码确实有效,但会从
dplyr
生成警告:

library(dplyr, warn.conflicts = FALSE)


# Copy built-in DF
my_iris <- iris

# Make random character vectors
rand_string1 <- sample(LETTERS, size = nrow(iris), replace = TRUE)
rand_string2 <- as.character(
    sample(100, size = nrow(iris), replace = TRUE)
)

# Fill new character columns in the DF. The second one is supposed to be casted
# to int
my_iris$A_rand_char <- rand_string1
my_iris$B_rand_char <- rand_string2

# Mutate: select all char columns **except** the ones whose name matches the
# regex, and make them numeric. In the example, only new_iris$B_rand_char should
# be affected
mutated_iris <- my_iris %>%
    mutate(
        # Get all char variables except 'A_rand_char' (see below) and ID code
        across(
            is.character & !matches('A_rand'),
            as.numeric
            ),
    )

# Old data
class(my_iris$A_rand_char)
#> [1] "character"

class(my_iris$B_rand_char)
#> [1] "character"


# New data
# Old character column(s) still character:
class(mutated_iris$A_rand_char)
#> [1] "character"

# Column(s) converted to numeric:
class(mutated_iris$B_rand_char)
#> [1] "numeric"
我猜这是错误的,因为我正在传递一个与
的返回值相交的函数!匹配(“A_rand”)
。但同样,当我使用
purr
风格的语法时,正如
where()
文档中的最后一个示例所示:

     where(~ is.character(.x) && !matches('A_rand'))
我得到:

Error: Problem with `mutate()` input `..1`.
x `where()` must be used with functions that return `TRUE` or `FALSE`.
ℹ Input `..1` is `across(where(~is.character(.x) && !matches(.x, "A_rand")), as.numeric)`.
所以现在的问题似乎是这两个函数返回的是与布尔向量不同的东西,我被卡住了,因为我真的认为它们应该这样做——特别是
matches()
,它在文档中被归类为选择帮助器

同样,代码的第一个版本确实有效,但会生成某种程度的弃用警告

除了名称与regexp匹配的字符列之外,选择所有字符列的更正确方法是什么


感谢所有能够贡献的人…

你们非常接近!以下是正确的语法:

mutated_iris <- my_iris %>%
  mutate(
    # Get all char variables except 'A_rand_char' (see below) and ID code
    across(
      where(is.character) & !matches('A_rand'),
      as.numeric
    )
  )
变异虹膜%
变异(
#获取除“A_rand_char”(见下文)和ID代码之外的所有字符变量
穿过(
其中(is.character)&!matches('A_rand'),
如:数字
)
)
您只需将
is.character
包装到
的where
这是因为
is.character
是一个谓词函数,而
where()
是一个选择帮助器。您需要在
where
中包装
is.character
,因为它不是选择帮助程序

这是您需要的代码:

变异虹膜%
变异(
#获取除“A_rand_char”(见下文)和ID代码之外的所有字符变量
穿过(
其中(is.character)&!matches('A_rand'),
如:数字
),
)
选择帮助程序严格适用于
dplyr
动词,如下错误所示

require(dplyr)
base::is.character(“hi”)
#>[1]是的
try(tidyr::matches(“hi”))
#>错误:`matches()`必须在*selecting*函数中使用。
#>我明白了。
try(where(is.character(“hi”))
#>where(is.character(“hi”)中出错:找不到函数“where”
TIBLE(a=character())%>%
变异(跨越(其中(是字符),修订))
#>#tible:0 x 1
#> # ... 带1个变量:a
由(v0.3.0)于2021-01-24创建

mutated_iris <- my_iris %>%
  mutate(
    # Get all char variables except 'A_rand_char' (see below) and ID code
    across(
      where(is.character) & !matches('A_rand'),
      as.numeric
    )
  )