From the list of data frame select particular columns in R -


i have multiple csv files 2, , want extract specific column each file.

filenames <- list.files("path") for(iter in 1:length(filenames)){   print(filenames[iter])   my_csv=read.csv(filenames[iter]) 

where, csv1 , csv2 contains columns a1,a2,a3,a4 , b1,b2,b3,b4 respectively. want fetch column a1 csv1 , b2 csv2 csv number (csv1(1 in case) matches coulmn name (a1 in case)). likewise b2 csv2(csv2 matches colummn name b2.

for reading in csv files recommend using fread data.table package (more on here).

library(data.table) filecsv <- list.files(pattern = "csv$") # function extracts column csv file # doesn't colnames getcsvcolumn <- function(file) {     <- gsub("csv|\\.", "", file)     foo <- fread(file)     foo[, paste0("v", i), = false] } res <- sapply(filecsv, getcsvcolumn) res <- do.call("cbind", res) colnames(res) <- gsub("\\..*","", colnames(res)) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -