Reputation: 21
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
Upvotes: 0
Views: 759
Reputation: 522626
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
Upvotes: 1
Reputation:
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, ]
Upvotes: 0