0

I have a column for Months. They are written in words and stored as factor. I want to get corresponding numeric values for them and I tried the following code but it is not working

Latest<-MasterFile
for(i in 1:nrow(MasterFile)){
  if(MasterFile[i,2]=="Januray"){
    MasterFile[i,2]=1
  }
    else if(MasterFile[i,2]=="February"){
      MasterFile[i,2]=2
    }
      else if(MasterFile[i,2]=="March"){
        MasterFile[i,2]=3
      }
        else if(MasterFile[i,2]=="April"){
          MasterFile[i,2]=4
        }
          else if(MasterFile[i,2]=="May"){
            MasterFile[i,2]=5
          }
             else if(MasterFile[i,2]=="June"){
               MasterFile[i,2]=6
             }
               else if(MasterFile[i,2]=="July"){
                 MasterFile[i,2]=7
               }
                  else if(MasterFile[i,2]=="August"){
                   MasterFile[i,2]=8
                  }
                     else if(MasterFile[i,2]=="September"){
                       MasterFile[i,2]=9
                     }
                        else if(MasterFile[i,2]=="October"){
                          MasterFile[i,2]=10
                        }
                          else if(MasterFile[i,2]=="November"){
                           MasterFile[i,2]=11
                          }
                            else if(MasterFile[i,2]=="December"){
                              MasterFile[i,2]=12
                             }
}
applepie
  • 9
  • 1

2 Answers2

3

Probably the month levels are in alpha order. Refactor them in the correct order before converting to numeric:

as.numeric(factor(MasterFile[, 2], levels = month.name))

This uses the built-in variable month.name to order the levels of the factor correctly in month order, at which point as.numeric converts the levels to their corresponding level numbers.

Some further reading that might help you: Vectorized if statement in R, (you should at least be using ifelse() rather than for() {if() else}), but there are often better approaches: Alternatives to nested ifelse in R

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
2

Another method is to use match with month.name as the second argument.

match(MasterFile[, 2], month.name)

This will return a vector of the length of the number of rows in MasterFile with the positions the the values there lie in the built-in constant month.name.

lmo
  • 37,904
  • 9
  • 56
  • 69