r - Load CSV's by Matching names and fetch specific Cols by maching Tag Names -


i have 2 data frames having "tagnames" , "filenames" , have csv files in directory. need open csv files 1 one using "filenames" fetch columns csv file matching "tagnames", append them "result" data frame , move next csv file (repeat). note: have take care of date , time because records coming different files must place according date , time.

tagnames , file names follows: tag names , file names

files directory , data looks this: files directory , data shape in csv

my r script this:

basepath <- dirname(rstudioapi::getactivedocumentcontext()$path) # load data basepath <- dirname(rstudioapi::getactivedocumentcontext()$path) filesdf <- read.csv("config/files.csv") tagsdf <- read.csv("config/tags.csv")  fileslist <- list(filesdf) tagslist <- list(tagsdf)  extractdata <- function(x) { result <- null; temp <- null; (i in 1:nrow(x)) { new_df <- read.csv(file=x$filenames[i,], header=true, sep=",") for(j in q:ncol(new_df)) {   temp <- rbind(temp, new_df[which(new_df[1,j])==tagslist$tag.names[i,]]) } result <- rbind(result, temp) temp <- null } return(result) } df_combined <- lapply(fileslist, extractdata) write.csv(df_combined, file = "ureasvr2.csv") 

in base r use like:

rbind(lapply(lapply(filelist, read.csv), subset, select = tagslist))

the inner lapply() reads in of files in list, outer 1 subsets data , uses select argument takes in vector of column names. finally, rbind puts list single data.frame.

i using purrr , dplyr myself though write more this:

map(filelist, read.csv) %>% map_df(select, tagnames)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -