I need to change a column from numeric to factor based on the numeric value being higher or lower than 10.
For example with the following data:
age <- c(1:20)
hight <- c(1:20)
d.frame <- data.frame(age, hight)
I tried the following:
d.frame$hight <- factor(d.frame$hight, levels( 1:9, 10:max(d.frame$hight) ), labels=c('low','high'))
and
d.frame$hight <- factor(d.frame$hight, levels( <10, >=10) ), labels=c('low','high'))
But don't work.
Any ideas now to do this type conversion?
Thanks
Creating a Factor in R Programming Language The command used to create or modify a factor in R language is – factor() with a vector as input. The two steps to creating a factor are: Creating a vector. Converting the vector created into a factor using function factor()
In R, factors are used to work with categorical variables, variables that have a fixed and known set of possible values. They are also useful when you want to display character vectors in a non-alphabetical order. Historically, factors were much easier to work with than characters.
Levels() function provides access to the levels attribute of a variable. The first form returns the value of the levels of its argument and the second sets the attribute.
We can check if a variable is a factor or not using class() function. Similarly, levels of a factor can be checked using the levels() function.
We could use cut
to change the numeric
to factor
column based on the condition
d.frame$hight <- cut(d.frame$hight, breaks = c(-Inf, 10, Inf),
labels = c('low', 'high'), right = FALSE)
As there are only two levels, another option would be to create a logical vector and use ifelse
to change the values
d.frame$hight <- factor(ifelse(d.frame$hight>=10, "high", "low"))
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