1

我有一个数据集,我想将它们分组到一个类别中......但我希望该类别大于和小于某个数字?

这是我的数据集。

"gridcellname","lat","lon","avg.temp"
"Y12X46",68.79,37.798,6.93684
"Y13X49",68.973,39.706,8.9314
"Y14X49",69.197,39.755,5.7867
"Y14X50",69.178,40.386,5.48162
"Y14X53",69.106,42.271,4.1848
"Y14X54",69.078,42.896,3.7789

我希望最后一列是这样的:

"gridcellname","lat","lon","avg.temp","temp_category"
"Y12X46",68.79,37.798,6.93684,(6>)
"Y13X49",68.973,39.706,8.9314,(6>)
"Y14X49",69.197,39.755,5.7867,(5,6]
"Y14X50",69.178,40.386,5.48162,(5,6]
"Y14X53",69.106,42.271,4.1848,(4,5]
"Y14X54",69.078,42.896,3.7789,(<4)
"Y40X44",75.102,37.191,1.99166,(<4)

我知道如何根据个人温度的中断进行分类。

df<- cbind(df, temp_category=cut(df$avg.temp, breaks=c(0,1,2,3,4,5,6,7)))

但我只想将 4、5、6 分组,其余的放入 <4 和 >6。这在R中可能吗?

4

1 回答 1

3

这行得通吗?

df<- cbind(df, temp_category=cut(df$avg.temp, breaks=c(-Inf,4,5,6,Inf)))
#the following is more idiomatic
df$temp_category <- cut(df$avg.temp, breaks=c(-Inf,4,5,6,Inf))
于 2013-02-13T17:09:37.807 回答