I have a column which is part of a data frame, df
. It is full of integers. Let's say it is the number of houses sold in a day by a reality compant. Let's call it df$houses
. I want to make a second column called df$quant
where the number of houses is categorized, with 0 being 0-2 houses sold in a day, 1 being 3-5 houses, 2 being 6-9 houses and 3 being more than 10 houses? I could do this in two steps.
1) Create the new column df$quant
from df$houses
:
df$quant <- df$houses
2) Change the values of df$quant
:
df$quant[which(df$quant <= 2)] <- 0
etc.
I would like to do this in one step though, making the new variable and filling it with the proper values. Mostly, so I don't have to worry about getting the order of the lines of code in the second step right. It would be more robust.
Could this be done with an if statement?
Thanks a lot.