[R] Extracting columns

Rui Barradas ruipbarradas at sapo.pt
Thu Nov 8 19:56:55 CET 2012


Hello,

Something like this should get you close.


filenames <- paste0("A", 1:22)
cols <- c('c1', 'c50', 'C750')

lapply(filenames, function(x){
     dat <- read.table(x, header = TRUE)
     subset(dat, select = which(names(dat) %in% cols))
})


Hope this helps,

Rui Barradas
Em 08-11-2012 15:50, Silvano Cesar da Costa escreveu:
> Hi,
>
> I have 22 files (A1, A2, ..., A22) with different number of columns,
> totaling 10,000 columns: c1, c2, c3, ..., c10000
>
> I have another file with a list of 100 columns that I need to extract.
> These 100 columns are distributed in 22 files.
>
> How to extract the 100 columns of the 22 files?
>
> I have done it "manually" with the following commands, for example:
>
> cromo1 = read.table ("~ / cromo1.raw ', head = T)
> c1 = subset (cromo1, select = c ('c1', 'c50', 'C750'))
>
> in this case, I know that the columns c1, c50 and C750 are on cromo1.raw.
> See who need to apply the commands above 22 times.
>
> Is there a way to schedule these operations?
>
>
> ---------------------------------------------
> Silvano Cesar da Costa
>
> Universidade Estadual de Londrina
> Centro de Ciências Exatas
> Departamento de Estatística
>
> Fone: (43) 3371-4346
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list