I am attempting to assign a number from 1 through 10 to a series of vectors based on what quantile they\'re in in a dataframe.
So far I have tried
quants
You might be better off using cut
rather than a loop:
Data = data.frame(Avg = runif(100))
quantpoints <- seq(0.1, 0.9, 0.1)
quants <- quantile(Data$Avg, quantpoints)
cutpoints <- c(-Inf, quants, Inf)
cut(Data$Avg, breaks = cutpoints, labels = seq(1, length(cutpoints) - 1))
This should work:
Data$quant <- for ( i in nrow(Data) ) {
Data$quant[1] <- ifelse(Data$Avg [i] < quants[1], 1, ifelse(Data$Avg [i] > quants[1] & Data$Avg[i] < quants[2], 2, 3))
}
Or equivalently (inside the for
loop):
if(Data$Avg [i] < quants[1])
Data$quant[1] <- 1
else{
if(Data$Avg [i] > quants[1] & Data$Avg[i] < quants[2])
Data$quant[1] <- 2
else
Data$quant[1] <- 3
}
You should assign the output of ifelse
conditions outside of it. That is:
output <- ifelse(a > b, a, b)