[R] automatic convert list to dataframe

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Oct 3 19:14:19 CEST 2022


Hello,


list2env(file1, envir = .GlobalEnv)


will create data.frames dx1, dx2, etc, in the global environment.
If you really need the names file1_dx1, file1_dx2, etc, you can first 
change the names


names(file1) <- paste("file1", names(file1), sep = "_")


and then run list2env like above.

Hope this helps,

Rui Barradas

Às 16:51 de 03/10/2022, Kai Yang via R-help escreveu:
> Hi R team,
> I can use rio package to read excel file into R as a list. The excel file content multiple sheets (30 - 40 data sheets). I can convert each data elements into dataframe manually. I have multiple excel files with multiple data sheets. I need to load them into R and do the comparison for same sheet name from difference excel file. My current code is:
>   library(rio)   setwd ("C:/temp")
> filenames <- gsub("\\.xlsx$","", list.files(pattern="\\.xlsx$"))
> for(i in filenames){
>    assign(i, import_list(paste0(i, ".xlsx", sep="")))
> }
> file1_dx1     <-  file1[["dx1"]]
> 
> file1_dx2     <-  file1[["dx2"]]
> 
> file1_dx3     <-  file1[["dx3"]]
> 
> file2_dx1     <-  file1[["dx1"]]
> 
> file2_dx2     <-  file1[["dx2"]]
> ......
> 
> I hope the code can automatic converting the list (may have 30 - 40 lists) by adding file name (such as: filename_sheetname) and put it in for loop
> 
> 
> Thank you,
> Kai
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list