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,821
  • 3
  • 36
  • 67
user288609
  • 12,465
  • 26
  • 85
  • 127
  • 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
  • 54,400
  • 17
  • 144
  • 178
  • I have tried x<-data.frame(read.table("x.csv",sep=",",header=T),stringsASFactors=FALSE). It did not work. For this case, some elements for the additional rows may not be existed in the data.frame to be added – user288609 Feb 04 '14 at 05:05
  • @user288609 - try `read.csv("x.csv", stringsAsFactors=FALSE)`. – thelatemail Feb 04 '14 at 05:11
  • How is the best way to add that addition row. I tried sth like temprow<-t(c("abc",rep("",10)) dataframe1[nrow(dataframe1)+1,]<-temprow, – user288609 Feb 04 '14 at 05:26
  • Something like: `dat <- rbind(dat, c("abc",rep("",10)))` maybe. – thelatemail Feb 04 '14 at 05:34