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
}
}
}
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 .
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.
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.
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.
cut(height, quantile(height, prob=c(203, 723, 74)/1000 ), labels=c("low", "medium", "high") )
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, ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With