Rstudio Extract from files from folder certain files and certain columns. (magrittr preferred)

260 Views Asked by At

The folder contains 200+ files. I only need to extract the data from 25. The files are all tab delimited. I only need 6 columns found in each of the 20 files. Member, Age, Address, City, State and zip. The columns appear in a different sequence but they do appear in each file. The age column might be 12 or 17. and the Member might appear in column 3 or 5 for example. So I need the files, apple.txt,cat.txt,dog.txt, test.txt as an example and then columns Member, Age, Address, City, State and zip

2

There are 2 best solutions below

1
On

This could help you:

list_of_files <- c("apple.txt","cat.txt","dog.txt","test.txt")

r <- NULL
for(name_file in list_of_files){
      x <- read.table(name_file, sep="\t", header = TRUE) %>%
            select(Member,Age, Address, City, State, zip)
      r <- rbind(r,x)
}
0
On

You can use the list.files() and reference the pattern= argument first only list the files that you want eg:

list_of_files <- list.files(folderpath,pattern=".txt") # or whatever key words youw ant)

From there, you can use the read_csv() function and the col_select arugment to only pull in the columns that you want

read_csv(list_of_files,col_select=c(Member,Age,Address,City,Sate,zim))

Thanks! and let me know if you found this helpful.