使用dplyr::filter运行代码时出现错误 “必须是逻辑向量,而不是数字5。”

huangapple go评论83阅读模式
英文:

running code with dplyr::filter gives error "must be a logical vector, not the number 5."

问题

以下是翻译好的部分:

我正在尝试运行这段代码:

data("ToothGrowth")
View(ToothGrowth)

filtered_tg <- filter(ToothGrowth, dose == 0.5)

但以下错误导致了问题:

Error in filter():
ℹ In argument: 5.
Caused by error:
! ..2 must be a logical vector, not the number 5.
Run rlang::last_error() to see where the error occurred.

我已经在RStudio控制台中运行了以下内容:

> install.packages("dplyr")
> library(dplyr)
英文:

I am trying to run this code:

data(&quot;ToothGrowth&quot;)
View(ToothGrowth)

filtered_tg &lt;- filter(ToothGrowth, dose == 0,5)

but the following error is causing me problems:

> Error in filter():
ℹ In argument: 5.
Caused by error:
! ..2 must be a logical vector, not the number 5.
Run rlang::last_error() to see where the error occurred.

I have already run the following in the RStudio console:

&gt; install.packages(&quot;dplyr&quot;)
&gt; library(dplyr)

答案1

得分: 3

有几种可能性。

  1. 这可能是一个简单的拼写错误,您本来想输入 0.5 而不是 0,5

  2. 您可能对小数点的约定感到困惑。这有相同的解决方案,但是是一个不同的概念性问题。

    R 使用北美约定,小数点是 ., 而不是 , 作为小数分隔符。请将剂量值指定为 0.5,而不是 0,5

filtered_tg <- filter(ToothGrowth, dose == 0.5)

由于 R 使用逗号表示其他含义,这是一个您无法更改的设置。(您 可以 为了读取和写入数据的目的进行更改,例如查看 read.csv2() 函数,或参考这里。)

  1. 您可能尝试为 dose 指定两个不同的可能值(在这种情况下,您应该使用 dose == 0 | dose == 5dose %in% c(0,5) 作为您的条件)。(这似乎不太可能,但有评论中提到。)
英文:

There are a few possibilities here.

  1. This is a simple typo, you meant to type 0.5 instead of 0,5.

  2. You are confused about decimal separator conventions. This has the same solution, but is a different conceptual problem.

    R uses the North American convention where ., not , is used as a decimal separator. Specify the dose value as 0.5, not 0,5.
    <!-- -->

filtered_tg &lt;- filter(ToothGrowth, dose == 0.5)

<!-- -->
As R uses a comma for lots of other things, this is a setting you can't change. (You can change it for the purpose of reading and writing data, e.g. see the read.csv2() function, or see here.)

  1. You are trying to specify two different possible values for dose (in which case you should use dose == 0 | dose == 5 or dose %in% c(0,5) as your criterion). (This seems implausible but was mentioned by commenters.)

答案2

得分: 0

filtered_tg <- filter(ToothGrowth, dose==0.5)
View(filtered_tg)

收到:

在 filter(ToothGrowth, dose == 0.5) 中出现错误:找不到对象 'dose'。

英文:

filtered_tg <- filter(ToothGrowth,dose==0.5)
View(filtered_tg)

received :

Error in filter(ToothGrowth, dose == 0.5) : object 'dose' not found
>

huangapple
  • 本文由 发表于 2023年2月19日 03:35:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定