aldorado
aldorado

Reputation: 4864

R transforms column to NA value - why?

I want to read in tab delimited files with one column indicating a class denoted by a "roman" enumeration (each file contains either i,ii,iii or iv in the second column). However, with files containing only "i", this column is transformed to NA values. I can reproduce this error with a small test file looking like this:

animal  class   info
dog i   a
cat i   a
rabbit  i   b

When I read it into R:

> d<-read.delim("test_roman.csv", sep="\t")
> d
  animal class info
1    dog    NA    a
2    cat    NA    a
3 rabbit    NA    b

What am I missing here? read.delim always worked for me on similar files.

Upvotes: 2

Views: 61

Answers (1)

martin
martin

Reputation: 3249

You could have found out if you had visualized the type:

typeof(d$class)
[1] "complex"

You clearly don't want to have a complex number, so we enforce the "character" class:

read.table("test_roman.csv", sep="\t", fill=T, colClasses="character", header=T)
# animal  class   info
# dog     i       a
# cat     i       a
# rabbit  i       b

If your file contains not only i but also ii and iv, it works nonetheless:

read.delim("test_roman2.csv", sep="\t")
#   animal class info
#1     dog     i    a
#2     cat     i    a
#3 rabbit      i    b
#4 rabbit     iv    b
#5 rabbit     ii    b

Upvotes: 5

Related Questions