1

My question is simple and there a lot of "answers" with lapply which give me something I am not looking for. They are not useful because I get a list and then I have to do another loop to obtain the dataframes, and I run into the same problem: create several dataframes at once with serialized names. That is why I am asking a new question.

I have ONE excel file with 5 different sheets, I want to create 5 different dataframes.

library(openxlsx)

ln=list()
for (i in 2:6) 
  {
ln[[i]]<-read.xlsx("File.xlsx", sheet=i-1, startRow=3)

}

Then, I know I can do:

mo_1<-data.frame(ln[[2]])

and it looks like it will give me the right sheet file. However, I need to do this for all the sheets. My attempt is:

for (i in 2:6){
  mo_[i]<-data.frame(ln[[i]])
}

and this brings me to the same problem as before. P

So I need 5 files: mo2, ...,mo6, and I want them to be DATAFRAMES (not lists).

Thank you.

JUST TO ADD why the other solutions do not work: -They do not seem to be making frames, they seem to be keep making lists and it seems I have to manually do the transformation.

Another way of phrasing this question is how to create data frames from a list using a loop.

Allan
  • 23
  • 1
  • 6
  • Mentioning that there're existing answers out there without explaining why they don't help, isn't useful. – Kalle Richter May 26 '17 at 14:29
  • Better, but still hard to say what you're referring to if you don't explain which measures the solutions which don't work include. – Kalle Richter May 26 '17 at 14:33
  • Your variable `ln` is a list of dataframes, and thus the easiest to access each dataframe with the `ln[[ ]]` notation. If you really want to use a sequence of new variable names then use the `assign` function. See related post: https://stackoverflow.com/questions/24699989/r-dynamically-create-a-variable-name . I believe: `assign(paste0("mo",i), ln[[i]])` is what you are looking for. – Dave2e May 26 '17 at 15:09
  • Thanks for the answer. However, what if I need 50 data frames, I can't be extracting 50 times by hand each data element in the list. I need a couple of lines that do that for me. Therefore, I wanted to create a loop so I can have 50 data frames. – Allan May 26 '17 at 15:11
  • If you want individual variable names place `assign(paste0("mo",i), ln[[i]])` inside your loop where `i` is the index value. If you want a vector of dataframes then just use your list variable `ln` and access each data frame with `ln[[ i ]]`. – Dave2e May 26 '17 at 15:22
  • The solution for Dave2e gave me exactly what I was looking for. Thank you. – Allan May 30 '17 at 17:08

1 Answers1

0

Reconsider an lapply solution as you can output elements of a named list of dataframes into separate objects using list2env. Just be sure to name the elements with setNames prior:

library(openxlsx)

ln <- lapply(2:6, function(i) read.xlsx("File.xlsx", sheet=i-1, startRow=3))    
ln <- setNames(ln, paste0("mou_", 1:5))

list2env(ln, envir=.GlobalEnv)

With that said, there's no real reason to work with separate objects as you can still use the list for df operations, especially as you comment there can be 50 dataframes which sound like they maintain similar structures. Just manage one object instead of many:

merge(ln$mou_1, ln$mou_2, by="id")

aggregate(.~group1 + group2, ln$mou_3, FUN=sum)

rbind(ln$mou_4, ln$mou_5)
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thank you very much for your answer, but this does not address the question. – Allan May 30 '17 at 17:07
  • Why doesn't this address the question? Did you run the code? As shown, `lapply` imports Excel sheets to a list of dataframes where `list2env` outputs list elements to separate dataframe objects. This exactly answers, *how to create data frames from a list using a loop.* By the way, `lapply` is a loop! – Parfait May 30 '17 at 18:07