R: Convert text (character) into integer type in a data frame

Wee picture Wee · Aug 2, 2016 · Viewed 13.8k times · Source

I'm now working on an input data frame :

Asset   Sector        Currency    MarketValue
 1       Financial       USD         100000
 2       Financial       USD         100000
...
100      Financial       USD         100000

While now I want to add a row to the data frame but with different sector:

101       Energy         USD         100000

I'm now trying the code:

newSector        <- rep(input[95,])
newSector$Sector <- "Energy"
input            <- rbind(input,newSector)

However, from the read.csv2() the sector column is read as integer type, while newSector$Sector <- "Energy" is just to change the cell to a character type "Energy", therefore I'm not able to rbind them together. R reports the error: "invalid factor level, NA generated".

I tried to do strtoi and as.integer but it reported "NAs introduced by coercion".

Could anyone help? Thanks a lot!

Answer

crazybilly picture crazybilly · Aug 2, 2016

It sounds to me like read.csv() is reading the Sector column is as a factor rather than an integer (or a character string, which is what you expect). R stores factors as integers under the covers which is why you're getting the invalid factor level, NA generated error.

You can confirm this by doing str(input) and looking at the classes of each column.

A couple possible solutions:

  1. use the stringsAsFactors=F argument when you use read.csv() to read in your original file. That way the Sector column comes in a a character column and you don't get confused (you can always convert Sector back to a factor later if you need that for your analysis.

  2. convert the existing input$Sector to a character column:

    input$Sector  <- as.character(input$Sector)