[英]Replace value with Factor in r data.table
I have a data set that has a column for a factor (in the example case, cut).我有一个数据集,其中有一列用于因子(在示例中为 cut)。 It is currently set up like this:
目前是这样设置的:
library(ggplot2) # access to diamonds dataset
library(data.table)
data <- data.table(diamonds)[,list(mean_carat=mean(carat)), by=c('cut', 'color')]
I am trying to change all entries named "Fair" to "Good" because in my data set, the two entries are actually the same thing but notated differently.我正在尝试将所有名为“Fair”的条目更改为“Good”,因为在我的数据集中,这两个条目实际上是相同的,但标记方式不同。 The syntax that I have been trying to use is:
我一直在尝试使用的语法是:
data[which(cut = "Fair"), cut := "Good"]
and the output is输出是
>Error: unexpected symbol in "data[which(cut = "Fair"), cut := "Good"]"
Can anyone tell me where I am going wrong?谁能告诉我我哪里出错了?
You used =
instead of ==
.您使用
=
而不是==
。 Try尝试
data[which(cut == "Fair"), cut := "Good"]
You also don't actually need the which
statement:您实际上也不需要
which
语句:
data[cut == "Fair", cut := "Good"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.