从r导出多个工作簿
使用以下图像展示我要做的事情。因此,基本上,我有2个工作簿,一个位置有2张。我想阅读R&执行两个操作(将0更改为Na&获取每列的平均值),然后以相同的方式将它们导出到同一位置(2个工作簿& 2工作表)。
操作的详细信息邮政导入工作簿&工作表为 a)在所有床单中将所有0转换为na& b)然后在每列末尾创建一个平均行。 (上图中突出显示的黄色)
在输出中突出显示的上面图像 yellow 是每列的平均值(帖子转换为0到Na)< /em>
我尝试在下面创建代码,但是问题是我不确定如何按照我导入的顺序将多个工作表导出回同一工作簿。
library(tidyverse)
library(readxl)
path <- "C:/Users/tme/Desktop/"
file_name <- list.files(pattern = "*.xlsx")
for (i in seq_along(file_name)){
sheet_nm <- excel_sheets(file_name[i])
for (j in seq_along(sheet_nm)){
assign(x = paste0(file_name[i],"_",sheet_nm[j]), value = read_xlsx(path = file_name[i], sheet = sheet_nm[j],col_names=FALSE), envir = .GlobalEnv)
}
}
Using below image to showcase what I am trying to do. So basically I have 2 workbook with 2 sheets at a location. I want to read them in R & perform two operation (Change 0 to NA & get average of each column) then export them back in the same manner to same location (2 workbook & 2 worksheet).
Details on Operation post import both workbook & worksheet are a) Convert all 0 to NA in all the sheets & b) then creating a average row at the end of each column. (Yellow highlighted in above image)
In the above image Yellow highlighted in the output are the mean values of each column (post converting 0 to NA)
I tried creating below code but the issue was I am not sure how to export multiple worksheet back to the same workbook basically in the order that I imported them.
library(tidyverse)
library(readxl)
path <- "C:/Users/tme/Desktop/"
file_name <- list.files(pattern = "*.xlsx")
for (i in seq_along(file_name)){
sheet_nm <- excel_sheets(file_name[i])
for (j in seq_along(sheet_nm)){
assign(x = paste0(file_name[i],"_",sheet_nm[j]), value = read_xlsx(path = file_name[i], sheet = sheet_nm[j],col_names=FALSE), envir = .GlobalEnv)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论