0

I am trying to read over 200 CSV files, each with multiple rows and columns of numbers. It makes most sense to reach each one as a separate data frame.

Ideally, I'd like to give meaningful names. So the data frame of store 1, room 1 would be named store.1.room.1, and store.1.room.2. This would go all the way up to store.100.room.1, store.100.room.2 etc.

I can read each file into a specified data frame. For example:

store.1.room.1 <- read.csv(filepath,...) 

But how do I create a dynamically created data frame name using a For loop?

For example:

for (i in 1:100){    
  for (j in 1:2){    
    store.i.room.j <- read.csv(filepath...)    
 }    
}    

Alternatively, is there another approach that I should consider instead of having each csv file as a separate data frame?

Thanks

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
ssaran
  • 13
  • 5
  • Don't store these as separate variables; put them in a list (or a nested list). Something like `lapply(1:10, function(i) lapply(1:2, function(j) read.table(paste("file",i,j,"csv", sep="."))))` – MrFlick Feb 05 '15 at 00:49

2 Answers2

1

You can create your dataframes using read.csv as you have above, but store them into a list. Then give names to each item (i.e. dataframe) in the list:

# initialize an empty list
my_list <- list()

for (i in 1:100) {
for (j in 1:2) {
df <- read.csv(filename...)
df_name <- paste("store", i, "room", j, sep="")
my_list[[df_name]] <- df
}
}

# now you can access any data frame you wish by using my_list$store.i.room.j
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you. This suggestion solved my problem exactly the way I wanted it. The one below is helpful as it enables me to analyze the data faster. – ssaran Feb 05 '15 at 19:35
0

I'm not sure whether I am answering your question, but you would never want to store those CSV files into separate data frames. What I would do in your case is this:

set <- data.frame()
for (i in 1:100){
    ##calculate filename here
    current.csv <- read.csv(filename)
    current.csv <- cbind(current.csv, index = i)
    set <- rbind(set, current.csv)

An additional column is being used to identify which csv files the measurements are from.

EDIT:

This is useful to apply tapply in certain vectors of your data.frame. Also, in case you'd like to keep the measurements of only one csv (let's say the one indexed by 5), you can enter

single.data.frame <- set[set$index == 5, ]
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37