0

I am trying to read a text file into R. I only want to import some selected variables. I know where they are located, say 15-18 are dob_yy, mob_mm is from 19 to 20. Anyway I can read such a file? In SAS,when I read a file, I could do the following input dob_yy 15-18 mob_mm 19-20 Any similar method in R?

Thanks!

  • Please check http://stackoverflow.com/questions/2193742/ways-to-read-only-select-columns-from-a-file-into-r-a-happy-medium-between-re – akrun Dec 03 '14 at 16:29

1 Answers1

2

The package data.table contains the fread function which can be used as an alternative to the base function read.table. It takes an argument select which does what you want (from ?fread):

  select: Vector of column names or numbers to keep, drop the rest.

So you would do fread(..., select = c(15:18, 19:20)) (alternatively you can specify the variable names instead of positions).

konvas
  • 14,126
  • 2
  • 40
  • 46