Does anyone know how to speed up running the following command? I want to replace the numerical “month” values with a character string … e.g. month 1 goes to “Jul”.
This command is really really slow as the dataframe I trying to implement it on is enormous!
for (i in 1:length(CO2$month)){
if(CO2$month[i]=='1') {CO2$months[i]<-'Jul'} else
if(CO2$month[i]=='2') {CO2$months[i]<-'Aug'} else
if(CO2$month[i]=='3') {CO2$months[i]<-'Sept'} else
if(CO2$month[i]=='4') {CO2$months[i]<-'Oct'} else
if(CO2$month[i]=='5') {CO2$months[i]<-'Nov'} else
if(CO2$month[i]=='6') {CO2$months[i]<-'Dec'} else
if(CO2$month[i]=='7') {CO2$months[i]<-'Jan'} else
if(CO2$month[i]=='8') {CO2$months[i]<-'Feb'} else
if(CO2$month[i]=='9') {CO2$months[i]<-'Mar'} else
if(CO2$month[i]=='10') {CO2$months[i]<-'Apr'} else
if(CO2$month[i]=='11') {CO2$months[i]<-'May'} else
if(CO2$month[i]=='12') {CO2$months[i]<-'Jun'}
}
You can do it without a loop and without if-else:
If your
monthcolumn isn’t really character, this is even easier: