ka chun chung ka chun chung - 2 months ago 19
R Question

R subset columns within a list

I have many data frame, so I import them as a list but I found that the number of columns of those data frames are different, so I decide I cut them off and I want to sort only columns 1 to 3 in all the data frames within the list.

list
[1]
TBBA-06-5415-01A-01D-1481-05 g01023169 0.956810536587318 BSL 7
TBBA-06-5415-01A-01D-1481-05 g03242877 0.0365834311749085 BSL 7
TBBA-06-5415-01A-01D-1481-05 g06008330 0.0400302305964602 BSL 7
TBBA-06-5415-01A-01D-1481-05 g08861569 0.0556296569207048 BSL 7
TBBA-06-5415-01A-01D-1481-05 g09748749 0.183017407923698 BSL 7
[2]
TBBA-62-A472-01A-11D-A24I-05 g02726808 0.860054268107413 OTB
TBBA-62-A472-01A-11D-A24I-05 g04243127 0.897393495417269 OTB
TBBA-62-A472-01A-11D-A24I-05 g10537079 0.875143441641266 OTB
TBBA-62-A472-01A-11D-A24I-05 g12497728 0.935925242951832 OTB
TBBA-62-A472-01A-11D-A24I-05 g13195463 NA OTB
[3]
TBBA-KK-A7AV-01A-11D-A32C-05 g02442630 0.862838460053133 AS 9 133318907
TBBA-KK-A7AV-01A-11D-A32C-05 g11045524 0.369049794350982 AS 9 133320781
TBBA-KK-A7AV-01A-11D-A32C-05 g13597397 0.235754213039205 AS 9 133320948
TBBA-KK-A7AV-01A-11D-A32C-05 g13835114 0.155860360642954 AS 9 133327530
TBBA-KK-A7AV-01A-11D-A32C-05 g14010829 0.668836842295365 AS 9
[N]
...

Desired output:
[1]
TBBA-06-5415-01A-01D-1481-05 g01023169 0.956810537
TBBA-06-5415-01A-01D-1481-05 g03242877 0.036583431
TBBA-06-5415-01A-01D-1481-05 g06008330 0.040030231
TBBA-06-5415-01A-01D-1481-05 g08861569 0.055629657
TBBA-06-5415-01A-01D-1481-05 g09748749 0.183017408
[2]
TBBA-62-A472-01A-11D-A24I-05 g02726808 0.860054268
TBBA-62-A472-01A-11D-A24I-05 g04243127 0.897393495
TBBA-62-A472-01A-11D-A24I-05 g10537079 0.875143442
TBBA-62-A472-01A-11D-A24I-05 g12497728 0.935925243
TBBA-62-A472-01A-11D-A24I-05 g13195463 NA
[3]
TBBA-KK-A7AV-01A-11D-A32C-05 g02442630 0.86283846
TBBA-KK-A7AV-01A-11D-A32C-05 g11045524 0.369049794
TBBA-KK-A7AV-01A-11D-A32C-05 g13597397 0.235754213
TBBA-KK-A7AV-01A-11D-A32C-05 g13835114 0.155860361
TBBA-KK-A7AV-01A-11D-A32C-05 g14010829 0.668836842
[N]
...


I am not familar with list in R. Thanks for your help.

Answer

We can use lapply to loop through the list and extract ([) the first 3 columns (1:3).

lapply(lst, `[`, 1:3)

If we are reading from files into a list, another option is select option in fread from data.table where we specify the number of columns to read.

Suppose, we want to read all the .csv files in the working directory

library(data.table)
files <- list.files(pattern = ".csv")
lst1 <- lapply(files, fread, select = 1:3) 
Comments