I have n numbers ranging from 0 to 0.6; I want to create a new column grouping this values with multiple conditional statements. If a number is between 0 - 0.1; name it = 0.1; from 0.1 - 0.2, group= 0.2; from 0.2 - 0.3, group=0.3; and so on.
value<- runif(20, min=0, max=0.6)
df<- data.frame(value)
I tried this way, but is not working, it is just assigning a correct answer for the first value, and not to the rest.
#Assigning groups for values
for (i in seq(1, length(df$value))) {
if (df$value[i] > 0 & df$value[i] <= 0.1 ){
df$group[i]<- "0.1"
} else if (df$value[i] > 0.1 & df$value[i] <= 0.2 ){
df$group[i]<- "0.2"
} else if (df$value[i] > 0.2 & df$value[i] <= 0.3 ){
df$group[i]<- "0.3"
} else if (df$value[i] > 0.3 & df$value[i] <= 0.4 ){
df$group[i]<- "0.4"
} else if (df$value[i] > 0.4 ){
df$group[i]<- "0.5"
}
return(df)
}
Any idea or a better way to do this. Thanks in advance.