Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R create factor based on condition

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

like image 940
Selrac Avatar asked Oct 01 '16 06:10

Selrac


People also ask

How do I create a factor data in R?

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()

How is factor () used in R?

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.

What does levels () do in R?

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.

How do you find the factors of a variable in R?

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.


1 Answers

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"))
like image 70
akrun Avatar answered Oct 07 '22 20:10

akrun