如何在R大于和小于R的类别中创建组数据?

时间:2013-02-13 17:07:14

标签: r

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

这是我的数据集。

"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分组,其余的分组放入&lt; 4和&gt; 6。这可能在R?

1 个答案:

答案 0 :(得分: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))