Home > database >  Iterate through multiple dataframes (csv), add a new column and assign a value drived from original
Iterate through multiple dataframes (csv), add a new column and assign a value drived from original

Time:04-01

I have a list of dataframes in .xyz and .csv extension. I tried iterating the .xyz (e.g.apalachicola_mile_76.xyz) extension and .csv files by applying the function;

split_func

that splits and take a unique value from the file name then copy it into a new column. However, it gives an error: "There were 30 warnings (use warnings() to see them)".

can only work with one data frame each at a time

# Load needed packages
library(tibble)
library(plyr) 
library(readr)

filez <- list.files('.', full.names = T, pattern = '*.xyz')
# create a function to assign values 

split_func <- function(mylist, df){
  split_first <- unlist(strsplit(mylist, '_mile_'))[2] #split the dataframe name,select a value
  split_sec <- unlist(strsplit(split_first, '\\.'))[1] 
  conv_num <- as.numeric(split_sec) #turn the selcted value to a number (integer)
  add_column(df, RM = conv_num) # create new column and add the number
  
}

#iterate the function over each csv file

# first read the exported data
filez_df <- list.files('.', full.names = T, pattern = '.csv') #import exported data 

#apply split_func function to all files

for(i in 1:length(filez_df)){ # iterate through the length of the file
  df_holder <- vector(mode = 'list', length = length(filez_df)) # create an empty list
  df_holder[i] <- split_func(filez[i], read.csv(filez_df[i])) # apply the function
  

CodePudding user response:

# Get paths to all .csv files in working dir
csvs <- list.files(pattern = ".csv")
xyzs <- list.files(pattern = ".xyz")

# Empty list to hold the result of each iteration
all_files <- list()

for(i in 1:length(csvs)){
  temp <- read.csv(csvs[i])
  mile_num <- sub(pattern = ".*_(\\d{ }).xyz", replacement = "\\1", x = xyzs[i])
  temp$mile <- mile_num
  all_files[[i]] <- temp
}

# Convert list of dataframes to a single dataframe
do.call(rbind, all_files)
  • Related