It seems to be a very simple flow control structure doubt, however I am having a hard time finding the correct syntax for this in R, I have tried numerous without success. I must be missing something really obvious.
I wanted to loop in a list with Brazilian states codes, and return the region it is in. My aim is to manipulate a larger data set, not a list, but here is a MWE using a list:
a <- c("RO", "AC", "AM" ,"RR", "PA", "AP", "TO", "MA", "PI", "CE", "RN", "PB", "PE", "AL", "SE", "BA", "MG", "ES", "RJ", "SP")
setregion <- function(uf) {
pb = txtProgressBar(min = 0, max = length(uf), initial = 0)
region_out<-list()
for (i in length(uf)) {
if (uf %in% c("RO" ,"AC" ,"AM" ,"RR", "PA" , "AP" , "TO")) {
region_out <- append(region_out,"North")
} else if ( uf %in% c("MA","PI","CE","RN","PB","PE","AL","SE","BA")) {
region_out <-append(region_out,"Northeast")
} else if ( uf %in% c("MG","ES","RJ","SP")){
region_out <- append(region_out,"Southeast")
} else if ( uf %in% c("PR", "SC", "RS")){
region_out <- append(region_out,"South")
} else if ( uf %in% c("MS","MT","GO", "DF")){
region_out <-append(region_out,"Midwest")
}
setTxtProgressBar(pb,i)
}
return(region_out)
}
setregion(a)
Upon running the above code, it seems the if loop breaks the for loop as well, and it only returns "North", which is the response to the very first item in the list.
I would expect a list with looking like:
"North", "North", "North" ,"North", "North", "North","North", "Northeast", "Northeast",...
- What am I missing?
Aucun commentaire:
Enregistrer un commentaire