I'm having trouble with an if statement. I have a data frame (data) with a column of district populations (pop82). There are three different population cutoffs, and I want to find which cutoff each district's population is closest to and then create a variable according to that answer:
low <- 10188
middle <- 13584
high <- 16980
limit.diff <- rep(NA, length(data$X))
dist.to.cutoff <- rep(NA, length(data$X))
pscore <- rep(NA, length(data$X))
for (i in length(data$X)){
low.diff <- abs(data$pop82 - low)
middle.diff <- abs(data$pop82 - middle)
high.diff <- abs(data$pop82 - high)
cutoff.diffs <- c(low.diff, middle.diff, high.diff)
closest.cutoff <- sort(cutoff.diffs)[1]
if (closest.cutoff == low.diff){
pscore[i] <- low.diff/low * 100
} else if (closest.cutoff == middle.diff){
pscore[i] <- middle.diff/middle * 100
} else if (closest.cutoff == high.diff){
pscore[i] <- high.diff/high * 100
} else if (closest.cutoff == low.diff & closest.cutoff == middle.diff){
pscore[i] <- low.diff/low * 100
} else if (closest.cutoff == middle.diff & closest.cutoff == high.diff){
pscore[i] <- middle.diff/middle * 100
}
}
I get an error: the condition has length > 1 and only the first element will be used.
Can anyone help me with this?
Thanks
Aucun commentaire:
Enregistrer un commentaire