0

Hi I'm trying to build a code that tries to do the following:

  1. lists all the files in a folder
  2. extracts the data from each of the excel files (each excel file has multiple sheets and I am using XLConnect package to pull the data from different sheets)
  3. reshape the data into the format that works best for my data analysis

Here is my code

for (i in 1:n) { 
  wb <- loadWorkbook(filelist[i]) 


  assign(paste("field",i,"_reportcontents",sep = ""),
         readWorksheet(wb, sheet="Report Contents"))

  assign(paste("field",i,"_company",sep=""),
         paste("field",i,"_reportcontents[31,3]",sep = ""))
}

The way the above code is set up, the second variable, which is field[i]_company is being set as the string "fieldi_reportcontents[31,3]" rather than the value that is in the dataframe field[i]_reportcontents.

How can I fix the code to achieve the values in the data frame rather than allocation of a string?

lmo
  • 35,764
  • 9
  • 49
  • 57
mvikred
  • 51
  • 1
  • 4

2 Answers2

1

Rather than use assign and pasting together variable names, I would use lists.

wb = lapply(filelist, loadWorkbook)
sheets = lapply(wb, readWorksheet, sheet = "Report Contents")
companies = lapply(sheets, "[", 31, 3) 

You could easily set the names of the lists, e.g.,

names(sheets) = sprintf("field_%s_reportcontents", seq_along(sheets))

But it isn't clear if this is necessary if you just use good object names with your lists.

See also How to make a list of data frames for more soapboxing about the benefits of lists.

Community
  • 1
  • 1
Gregor Thomas
  • 104,719
  • 16
  • 140
  • 257
  • Although not exactly what i was looking for this code is very elegant and I will make use of it in mine. Thanks !! – mvikred Jan 20 '17 at 08:59
0

Just assign the sheet being read to a temporary variable. Also, you can replace paste with sep = "" by paste0, it's the same.

for (i in 1:n) { 
  wb <- loadWorkbook(filelist[i]) 

  temp <- readWorksheet(wb, sheet="Report Contents")

  assign(paste0("field",i,"_company"),
         temp)
}

As for why your last assign statement isn't doing what you want, I'll point you to an answer I wrote yesterday. Basically, the value you are trying to assign, paste("field",i,"_reportcontents[31,3]",sep = ""), is just a string. You can use get with a string to use a variable, but in your case you aren't trying to assign a variable either because you are also using the function [, so you would need to parse and evaluate the string you construct. In the end, the list approach is a better way to go.

Community
  • 1
  • 1
Paulo MiraMor
  • 1,482
  • 12
  • 27
  • This works well for me. I've also researched and found mget to be a relevant function here as well. Thanks ! – mvikred Jan 20 '17 at 09:00