Reputation: 109
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.
Upvotes: 0
Views: 102
Reputation: 430
The cut
function can convert the numeric vector into a factor:
df$group = cut(df$value,
breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6),
labels = c('0.1','0.2','0.3','0.4','0.5','0.6'))
head(df)
# value group
#1 0.4204752 0.5
#2 0.4691363 0.5
#3 0.3926192 0.4
#4 0.0418944 0.1
#5 0.1074853 0.2
#6 0.1914169 0.2
levels(df$group)
#[1] "0.1" "0.2" "0.3" "0.4" "0.5" "0.6"
Upvotes: 1
Reputation: 388817
You can use plyr
s round_any
function
df$value1 <- plyr::round_any(df$value, 0.1, ceiling)
df
# value value1
#1 0.59465953 0.6
#2 0.10581043 0.2
#3 0.48806113 0.5
#4 0.04106798 0.1
#5 0.24026985 0.3
#6 0.08468660 0.1
#7 0.11598592 0.2
#8 0.50481103 0.6
#9 0.43194839 0.5
#10 0.16032725 0.2
#11 0.29700099 0.3
#12 0.04986834 0.1
#13 0.21233054 0.3
#14 0.58152528 0.6
#...
Upvotes: 3