在R中将连续范围更改为分类 [英] Changing Continuous Ranges to Categorical in R

查看:90
本文介绍了在R中将连续范围更改为分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些连续的整数转换为分类范围,但是发生了一些我不了解的事情。尽管我一心想要得到想要的东西,但我仍然不明白为什么会这样。

I was trying to convert some continuous integers to categorical ranges, but something I did not understand happened. Although I fixed to get what I want, I still don't understand why it happened.

变量是0到12之间的一些整数,下面的代码还剩下 10 5 + 类别中的11 12

The variable is some integers from 0 to 12, the following code left 10,11,12 out from the 5+ category.

py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==0]<-"0"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==1]<-"1"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==2]<-"2"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==3]<-"3"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==4]<-"4"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain>=5]<-"5+"
py2$Daily.Whole.Grain<-as.factor(py2$Daily.Whole.Grain)

但是当我更改转换顺序时,它包括 10 11 12

But when I change the order of conversion, it includes 10,11,12.

py2$Daily.Whole.Grain[py2$Daily.Whole.Grain>=5]<-"5+"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==0]<-"0"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==1]<-"1"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==2]<-"2"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==3]<-"3"
py2$Daily.Whole.Grain[py2$Daily.Whole.Grain==4]<-"4"

任何人都可以解释一下,为什么将两位数整数留在外面?
非常感谢。

Can anyone explain it, why it leaves double digits integers out? Thanks very much.

推荐答案

如@CathG所述,问题是由于将列从数字归类为字符。这也许是使用cut函数的更好解决方案,它将基于变量的割点为您提供因子:

As @CathG mentioned, the problem is due to converting the column from a numeric class to character. Here is perhaps a better solution using the cut function which will give you factors based on cut-points of a variable:

py2 <- data.frame(Daily.Whole.Grain = 1:10)
py2$Daily.Whole.Grain1 <- cut(py2$Daily.Whole.Grain, 
    breaks = c(1:5, Inf), right = FALSE, labels = c(1:4, "5+"))
py2
   Daily.Whole.Grain Daily.Whole.Grain1
1                  1                  1
2                  2                  2
3                  3                  3
4                  4                  4
5                  5                 5+
6                  6                 5+
7                  7                 5+
8                  8                 5+
9                  9                 5+
10                10                 5+

这篇关于在R中将连续范围更改为分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆