-2

I have a .txt file with 40 columns and 1500 rows , the column headings are given in the top as follows:

col1 col2 col3 ... col40
 12   13   14  ...  100
...

How can I convert it into a matrix and extract only column 30 in R

user2100721
  • 3,557
  • 2
  • 20
  • 29
jane_94
  • 1
  • 2
  • Possible duplicate of [Reading text files using read.table in R](http://stackoverflow.com/questions/1407647/reading-text-files-using-read-table-in-r) – user2100721 Oct 05 '16 at 09:50
  • 1
    That's not a big file. Why can't you read it in with standard tools and just pull out column 30 from the resultant data frame? – hrbrmstr Oct 05 '16 at 11:21

1 Answers1

0

To do it:

txt.file <- read.table(file = 'path_to_your_file'/file.txt, header = TRUE, stringsAsFactors = FALSE)
txt.file.matrix <- as.matrix(txt.file)

There are many ways to extract a certain number of columns in R. In order to extract only 30, you could try to run:

only.30.columns <- txt.file.matrix[, 1:30] #or any other range you'd like
Fábio
  • 771
  • 2
  • 14
  • 25