R interpreting variable in data frame as factor; it isn't

前端 未结 2 467
轮回少年
轮回少年 2021-01-22 10:41

I imported a csv into R and it considers my variable FERNH a factor; it isn\'t. I can\'t figure out why or how to fix it. I have reviewed the values in FERNH and they appear t

相关标签:
2条回答
  • 2021-01-22 11:13

    There is a character value in there somewhere so R is acting as expected. Look at levels(height$FERNH) to see the offending value. You can set stringsAsFactors=FALSE on your read step or via options, but then the column will be a character rather than integer.

    While that may seem annoying at first, it is an excellent data quality check as well as significant memory savings if the character column contains long strings.

    0 讨论(0)
  • 2021-01-22 11:21

    @Justin's pinpointed the problem. Rather than trying to find your offending value with levels, though, you could supply colClasses='integer' to read.csv. Then R will raise an error when it encounters the value during reading and report what it is.

    0 讨论(0)
提交回复
热议问题