Qiang Li
Qiang Li

Reputation: 10865

data.frame does not parse correctly in R

I used this to parse a data frame

mydf <- read.table(header = FALSE, text = "1427140800000 14
1427184000000 NULL")

However, it shows

> mydf[1,2]
[1] 14
Levels: 14 NULL

But I am expecting 14. So how can I fix it?

Upvotes: 0

Views: 64

Answers (1)

jeborsel
jeborsel

Reputation: 697

Dataframe does parse correctly but I guess you were not expecting it to coerce it automatically to factors. Because that is what Levels: 14 NULL , is telling you: there are 2 factor levels 14 & NULL & the value of the factor mydf[1,2] is 14)

To avoid coercing to factors:

 mydf <- read.table(header = FALSE, text = "1427140800000 14
 1427184000000 NULL",stringsAsFactors=F)

And in case you want it as numeric values and not as strings:

 mydf[,2]<-as.numeric(mydf[,2])

Upvotes: 1

Related Questions