[英]R: Iterating over a df to paste one slice at a time into MS Excel Spreadsheet using lapply and seq_along
问题:我正在尝试自动将数据输入到执行计算的旧版 MS Excel 电子表格中。 我已经弄清楚如何使用 openxlsx 一次处理一个“切片”数据。 我想修改下面的代码来迭代整个数据集,而不是做这顿饭。
#NO ITERATION
#load libraries and define working directory
if(!require(tidyr)){install.packages("tidyr")}
if(!require(openxlsx)){install.packages("openxlsx")}
setwd("C:/R/Seq_along")
#Sample data frame
Site <- rep(letters[1:6],each=3)
Param <- as.factor(rep(c("X","Y","Z"),6))
set.seed(71)
Result <- sample(0:25, 18, replace = TRUE)
df <- data.frame(Site,Param,Result)
df
str(df)
#Pivot from long to wide format
df.long <- df %>% pivot_wider(names_from = Site, values_from = Result)
#Define list of working slices and workbook sheets
slicelist <- list(c(2:4),c(5:7))
#sheetlist <- list(c("Sheet1"),c("Sheet2"))
#Slice #1
#Slice df into first slice
slicecurrent <- slicelist[[1]]
#sheetcurrent <- sheetlist[[1]]
df1 <- df.long[,slicecurrent]
#Loadworkbook and write first slice into sheet 1 of workbook
wb1 <- loadWorkbook("test.xlsx")
class(wb1)
names(wb1)
writeData(wb1,sheet="Sheet1", x = df1, xy = c(2,6), colNames = FALSE, rowNames = FALSE)
saveWorkbook(wb1,"test.xlsx", overwrite = TRUE)
#openXL("test.xlsx")
#Slice #2
#Slice df into first slice
slicecurrent <- slicelist[[2]]
#sheetcurrent <- sheetlist[[2]]
df1 <- df.long[,slicecurrent]
#Loadworkbook and write first slice into sheet 1 of workbook
wb1 <- loadWorkbook("test.xlsx")
class(wb1)
names(wb1)
writeData(wb1,sheet="Sheet2", x = df1, xy = c(2,6), colNames = FALSE, rowNames = FALSE)
saveWorkbook(wb1,"test.xlsx", overwrite = TRUE)
openXL("test.xlsx")
我正在使用的那些数据需要被切成“切片”(这里使用 2 个切片),因为电子表格一次只能处理一定数量的数据列(在本例中为 3)。 要粘贴到电子表格上的实际数据称为“df.long”,而测试电子表格是一个名为“test.xlsx”的空白电子表格,保存在工作目录中。
这是我所追求的结果的图像: https://ibb.co/SJ0xG4Q
我尝试将 lapply 与 seq_along function 结合使用来指定要使用的切片并迭代该过程。 然而。 而不是像在无迭代脚本中那样将第一个块粘贴到电子表格中的 sheet1 和电子表格的 sheet2 中的第二个块,它遍历列表的所有元素,我最终将相同的块粘贴到两个 sheet1 和sheet2(我已经告诉它这样做)。
#load libraries and define working directory
if(!require(tidyr)){install.packages("tidyr")}
if(!require(openxlsx)){install.packages("openxlsx")}
setwd("C:/R/Seq_along")
#Sample data frame
Site <- rep(letters[1:6],each=3)
Param <- as.factor(rep(c("X","Y","Z"),6))
set.seed(71)
Result <- sample(0:25, 18, replace = TRUE)
df <- data.frame(Site,Param,Result)
df
str(df)
#Pivot from long to wide format
df.long <- df %>% pivot_wider(names_from = Site, values_from = Result)
#Define list of working slices and workbook sheets
slicelist <- list(c(2:4),c(5:7))
#sheetlist <- list(c("Sheet1"),c("Sheet2"))
lapply(seq_along(slicelist),
function(i){
#Slice #1
#Slice df into first slice
#slicecurrent <- slicelist[[1]]
#sheetcurrent <- sheetlist[[1]]
df1 <- df.long[,slicelist[[i]]]
#Loadworkbook and write first slice into sheet 1 of workbook
wb1 <- loadWorkbook("test.xlsx")
class(wb1)
names(wb1)
writeData(wb1,sheet="Sheet1", x = df1, xy = c(2,6), colNames = FALSE, rowNames = FALSE)
saveWorkbook(wb1,"test.xlsx", overwrite = TRUE)
#openXL("test.xlsx")
#Slice #2
#Slice df into first slice
#slicecurrent <- slicelist[[2]]
#sheetcurrent <- sheetlist[[2]]
df1 <- df.long[,slicelist[[i]]]
#Loadworkbook and write first slice into sheet 1 of workbook
wb1 <- loadWorkbook("test.xlsx")
class(wb1)
names(wb1)
writeData(wb1,sheet="Sheet2", x = df1, xy = c(2,6), colNames = FALSE, rowNames = FALSE)
saveWorkbook(wb1,"test.xlsx", overwrite = TRUE)
})
openXL("test.xlsx")
有没有办法我可以使用 lapply 和 seq_along function 复制第一个脚本的结果,还是应该使用 for 循环?
1: https://i.stack.imgur.com/XSmhT.jpg
要么您不知道如何使用lapply()
,要么您不知道如何使用openxlsx
。 无论如何,这里是一个仅使用基本 R 和openxlsx
的清理工作示例。 此外,对于这样的事情,我会使用一个简单for
循环。
library(openxlsx)
#Sample data frame
Site <- rep(letters[1:6],each=3)
Param <- as.factor(rep(c("X","Y","Z"),6))
set.seed(71)
Result <- sample(0:25, 18, replace = TRUE)
df <- data.frame(Site,Param,Result)
#Pivot from long to wide format
df.long <- reshape(df, idvar = "Param", timevar = "Site", direction = "wide")
#Define list of working slices and workbook sheets
slicelist <- list(c(2:4),c(5:7))
# wb1 <- loadWorkbook("test.xlsx") # I do not have this file
wb1 <- createWorkbook("test.xlsx")
out <- lapply(
seq_along(slicelist),
function(i) {
dfi <- df.long[,slicelist[[i]]]
sheet <- paste0("Sheet", i)
addWorksheet(wb1, sheet) # I add the sheet. Remove this line if you have the sheets
writeData(wb1, sheet = sheet, x = dfi, xy = c(2,6), colNames = FALSE, rowNames = FALSE)
})
saveWorkbook(wb1,"test.xlsx", overwrite = TRUE)
openXL("test.xlsx")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.