0

I have a table containing mixed values, including character string and numerical value. I chose to use a data.frame to store it. However, I have met serious difficulties in adding extra rows to the data frame. Error messages, like invalid factor level, NA generated, keep occurring.

Besides using a data.frame, are there any data structure that can help avoid the issue of invalid factor level, NA generated, while still supporting the mixed data format?

FXQuantTrader
  • 6,541
  • 3
  • 35
  • 66
user288609
  • 11,491
  • 24
  • 77
  • 114
  • Possible duplicate of [Add row to dataframe](https://stackoverflow.com/questions/28467068/add-row-to-dataframe) – phiver May 07 '19 at 07:56

1 Answers1

0

data.frame (or data.table) would most likely me the data structure you are looking for.

To put it plainly: in order to add "a new row", every new element needs to confirm to whatever restrictions pertain to its column. Mostly, that means being of the same class.

If you are adding elements to a factor column (as the error you received indicates) then there is an additional requirement that the new values must also be levels of that factor. (see ?factor for more info)

If one is using factors deliberately, then the above is a good thing. However, if one has a column that was unintentionally coerced to a factor, then the above is a P.I.T.A.

Unfortunately, the default on most functions that generate data.frames is to have stringsAsFactors=TRUE. This, imho, is annoying. But all you have to do is turn that flag off, and you should be set.

Ricardo Saporta
  • 52,793
  • 14
  • 136
  • 168