See ?cut
and ?findInterval
.
df$CDrange <- cut(df$Concentration, c(700000, 800000, 900000, 1000000, Inf),
labels=c("700k+","800k+","900k+","1000k+") right=FALSE)
Pregunta
I have a data frame with a colmn called concentration with numeric values.
Concentration
700657
850789
900123
1011234
750001
I want to add a new column CDrange that has values 700k+,800k+,900k+,1000k+. The values in the new column will be assigned based on the value of concentration, e.g. if value is 700657, the calculated value should be 700k+, if value is 850789 then value as 800k+ and so on. How do I write a function without using if else loops. The new column should look like
Concentration CDrange
700657 700k+
850789 800k+
900123 900k+
1011234 1000k+
750001 700k+
Please provide on some suggestions on how to proceed. I have tried using subset function but I am unable to keep the dataframe together.
Solución
See ?cut
and ?findInterval
.
df$CDrange <- cut(df$Concentration, c(700000, 800000, 900000, 1000000, Inf),
labels=c("700k+","800k+","900k+","1000k+") right=FALSE)
Otros consejos
Check this example:
x <- c(800000, 800001, 800999, 1234567)
paste0(x %/% 1000, ifelse((x %% 1000)>0, "k+", "k"))
#[1] "800k" "800k+" "800k+" "1234k+"