r - Performance of a loop for modifiying a factor -


so have following loop:

for(i in 1:dim(d)[1]) {   if(d$countryname[i] %in% c("italy","spain","canada","brazil","united states","france","mexico","colombia","peru","chile","argentina","ecuador"))    {next}    else    {d$countryname[i] <- "others"} } 

the "d" dataframe has more 6,5 million rows , d$countryname factor.

is there way make faster? very slow. thank you.

how about:

log <- d$countryname %in% c("italy","spain","canada","brazil","united states","france","mexico","colombia","peru","chile","argentina","ecuador")  d$countryname[!log] <- "others" 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -