英文:
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.
Runrlang::last_error()
to see where the error occurred.
我已经在RStudio控制台中运行了以下内容:
> install.packages("dplyr")
> library(dplyr)
英文:
I am trying to run this code:
data("ToothGrowth")
View(ToothGrowth)
filtered_tg <- 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:
> install.packages("dplyr")
> library(dplyr)
答案1
得分: 3
有几种可能性。
-
这可能是一个简单的拼写错误,您本来想输入
0.5
而不是0,5
。 -
您可能对小数点的约定感到困惑。这有相同的解决方案,但是是一个不同的概念性问题。
R 使用北美约定,小数点是
.
, 而不是,
作为小数分隔符。请将剂量值指定为0.5
,而不是0,5
。
filtered_tg <- filter(ToothGrowth, dose == 0.5)
由于 R 使用逗号表示其他含义,这是一个您无法更改的设置。(您 可以 为了读取和写入数据的目的进行更改,例如查看 read.csv2()
函数,或参考这里。)
- 您可能尝试为
dose
指定两个不同的可能值(在这种情况下,您应该使用dose == 0 | dose == 5
或dose %in% c(0,5)
作为您的条件)。(这似乎不太可能,但有评论中提到。)
英文:
There are a few possibilities here.
-
This is a simple typo, you meant to type
0.5
instead of0,5
. -
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 as0.5
, not0,5
.
<!-- -->
filtered_tg <- 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.)
- You are trying to specify two different possible values for
dose
(in which case you should usedose == 0 | dose == 5
ordose %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
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论