beta
beta

Reputation: 5686

read multiple CSV and add to list in a loop in R

I have multiple CSV files and want to read them in R. the file names are provided as an argument, so I don't know the names in advance. This is the reason, why I do it in a loop.

Next, I want each dataframe to be appended to a list. As a result, I want to have an indexed list to access access the first line of all dataframes and I think the best way to do this is to have a list of the dataframes available. If there is a more efficient way to do this, any solutions are appreciated.

But for now I will go for the having all dataframes in a list approach. For this I would like to know how I can add a dataframe to a list in a loop.

I have this code:

#splitting the args to get the filenames
splat <- strsplit(args[1], ",")[[1]]
for (fname in splat) {
  #d.fname = dataframe
  d.fname <- read.csv(fname, header = FALSE, sep = ",", dec = ".")
  #code needed to add d.fname to a list?
}

So all in all I have two questions: 1) how can I add dataframes to a list in a loop? 2) is there a better way to do this, with the goal in mind, that later I need to have access to all dataframes like do something (e.g. create a box plot) with the first elements of the first rows of all dataframes?

I am aware of solutions like this: Importing multiple .csv files into R . However, I cannot apply this solution, since I am not able to use list.files(pattern="*.csv") because I dont know the filenames in advance.

Upvotes: 0

Views: 1043

Answers (3)

t.jirku
t.jirku

Reputation: 35

That might help - too

# instantiate an empty list
ldf <- list()

# creates the list of all the csv files in the directory 
# filter for filenames holding UnixM
listcsv <- dir(pattern = ".UnixM.") 

# loop thorugh the list holding the single CSV files
for (k in 1:length(listcsv)){
   ldf[[k]] <- read.csv(file = listcsv[k]) 
}

Upvotes: 1

dougmet
dougmet

Reputation: 685

To answer the first part of your question, you can append to lists in a loop with

# Create an empty list
csvList <- list()
#splitting the args to get the filenames
splat <- strsplit(args[1], ",")[[1]]
for (fname in splat) {
  #d.fname = dataframe
  d.fname <- read.csv(fname, header = FALSE, sep = ",", dec = ".")
  #code needed to add d.fname to a list?
  csvList[[length(csvList)+1]] <- d.fname
}

But I agree lapply is much better and will carry the names through. This can be a useful method for more complicated appending when lapply might start to struggle.

Upvotes: 2

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

On your original question, you can use directly:

lapply(strsplit(args[1], ",")[[1]], read.csv, header=F, sep=',', dec='.')

Upvotes: 3

Related Questions