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
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
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