converting a vector of character strings to separate objects
1
0
Entering edit mode
5.1 years ago
Assa Yeroslaviz ★ 1.8k

I am trying to automate the way i read my tables. I have an Excel sheet I'm reading using openxlsx package. The sheet contains over 30 sheets, i would like to save each of them as separate objects.

my workflow for now is as such:

wb <- loadWorkbook(xlsxFile = "Output/Up_Down_Regulated_Gene_Lists.xlsx") 
NAMES <- gsub(pattern = " ", replacement = "_", x = names(wb))
[1] "KO1_vs._WT_up"    "KO2_vs._WT_down"  "KO3_vs._WT_up"    "KO1_vs._WT_down"  "KO2_vs._WT_up" ...


for (i in 2:length(names(wb)) ){
  tmp <- read.xlsx(wb, sheet = i)

... Here I would like to have each sheet read in and sacved as a separate data.frame.

}

Is there a way to read for each sheet the name from names(wb) and convert it to a name for the object?

something like this

The object KO1_vs._WT_up will save the first sheet with the same name

The object KO2_vs._WT_down will save the second sheet.

etc.

Any ideas?

thanks

R • 618 views
ADD COMMENT
1
Entering edit mode
5.1 years ago
zx8754 11k

Try:

myDfList <- lapply(names(wb), function(i) read.xlsx(wb, i))

I prefer to keep all dataframes in a list, but if we wish to clutter our environment with zillion dataframes then:

list2env(myDfList, envir = .GlobalEnv)
ADD COMMENT

Login before adding your answer.

Traffic: 1563 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6