-1

I am having trouble importing data correctly into r from a txt file with missing values. When I import, the column of numeric values with the missing value (have tried it both as "." and "Na") is read as a factor variable. The import code I use is:

dat2 <- read.table(file.choose(), header = T)

Looking at the structure of the data, I see for the column of interest:

adultreg    : Factor w/ 19 levels "1.85","101.75",..: 11 15 15 1 13 6 17 9 16 3 

After converting the factor to the numeric variable (dat2$adultreg<-as.numeric(dat2$adultreg), I ask r to output the data just so I can see what it is, it is not at all the data from the text file anymore.

joran
  • 169,992
  • 32
  • 429
  • 468

1 Answers1

1

Factors in R are tricky - they are actually stored as integers, where each integer is decoded based on what are called levels.

The most intuitive method for conversion is as.numeric(as.character(dat2$adultreg))

The fastest way (I believe) is levels(dat2$adultreg)[dat2$adultreg]

Señor O
  • 17,049
  • 2
  • 45
  • 47
  • thanks. that does help. but now when i try to create a linear model with the variables containing missing values i get an error saying there are missing values in the objects. how should i mark these in my original data so that this doesn't happen? or is it in the import commands that i deal with this appropriately? – user2880353 Oct 14 '13 at 21:10
  • 1
    @user2880353 this comment is not related to the original question. AVOID asking many questions in the same OP. That's said , You need only to remove your missings values. – agstudy Oct 14 '13 at 21:15