Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change discrete ratio data into ordinal data in R?

Tags:

r

Here is an example:

   height
1  1.5
2  1.3 
3  1.9 
4  1.5
5  1.6 

There are 1000 of them with height ranging from 0 to 1.9. And I want to cut them into 3 levels: low, medium and high. Then they are ordinal data.

result should look like this:

   height
1  medium
2  low
3  high
4  medium
5  medium

And the summary should look like:

        height
low:    203
medium: 723
high:   74

I tried to use the loop but then "low, medium and high" are characters, not levels. Here is how I did the low part:

height_cuts = c(1.5,1.9)
for(i in 1:nrow(health.sample)){
  if(is.na(health.sample$height[i])==FALSE){
    if(health.sample$height[i] < height_cuts[1]){
      health.sample$height[i] = low_h
    }
  }
}
like image 461
offrampq Avatar asked Oct 19 '11 00:10

offrampq


People also ask

How do I change data type to ordinal in R?

The factor() function also allows you to assign an order to the nominal variables, thus making them ordinal variables. This is done by setting the order parameter to TRUE and by assigning a vector with the desired level hierarchy to the argument levels .

How do you convert a ratio variable to an ordinal variable?

Suppose we observe height (in). This is a ratio variable. We can reduce it to ordinal, by redefining it as height (short, medium, tall), where short means less than 60 inches, medium means between 60 and 72 inches, and tall means greater than 72 inches.

Can ratio data be converted into ordinal?

Interval or ratio measurements can also be changed into ordinal scale measurements by simply ranking the observations. A number of nonparametric statistical methods are, in fact, based on ranks.

How do you convert nominal data to ordinal data?

A nominal scale cannot be transformed into an ordinal or interval scale. There is no order in a nominal scale but there is in an ordinal or interval scale. You can go the other way around, i.e., interval or ordinal scale converted to a nominal scale, but you will be throwing information away. Thank you Dr.


2 Answers

cut(height, quantile(height, prob=c(203, 723, 74)/1000 ), labels=c("low", "medium", "high") )
like image 189
IRTFM Avatar answered Oct 17 '22 21:10

IRTFM


cut will, conveniently enough, cut your data.

# cut needs all endpoints explicitly specified, including outside bounds
height_cuts <- c(-Inf, 1.5, 1.9, Inf)

hcut <- cut(height, height_cuts, labels=c("low", "medium", "high"))

ETA: this will make intervals based on <=1.5, <=1.9. If you want the intervals to be <1.5, <1.9, specify right=FALSE:

hcut <- cut(height, height_cuts, right=FALSE, ...)
like image 24
Hong Ooi Avatar answered Oct 17 '22 19:10

Hong Ooi