用r data.table中的Factor替换值 [英] Replace value with Factor in r data.table
本文介绍了用r data.table中的Factor替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集,其中包含一个因子(在示例中为cut)的列。它目前设置如下:
库(ggplot2)#访问钻石数据集
库)
data< - data.table(diamonds)[,list(mean_carat = mean(carat)),by = c('cut','color')]
我试图将名为Fair的所有条目更改为Good,因为在我的数据集中,这两个条目实际上是相同的,不同。我一直试图使用的语法是:
data [which(cut =Fair),cut:= Good]
,输出为
>错误:data [which(cut =Fair),cut:=Good]中的意外符号
pre>
任何人都可以告诉我我在哪里出错?
解决方案您使用
=
而不是==
。尝试data [which(cut ==Fair),cut:=Good]
你也不需要
其中
语句:data [cut ==Fair,cut:=Good]
pre>
I have a data set that has a column for a factor (in the example case, 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. 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==
. Trydata[which(cut == "Fair"), cut := "Good"]
You also don't actually need the
which
statement:data[cut == "Fair", cut := "Good"]
这篇关于用r data.table中的Factor替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文