将不同数据框中的多列重命名为一个对象
Renaming multiple columns in different data frames combined into one object
我有 65 excel 个文件存储在一个文件夹中(即 JanuaryFiles)。我需要:
- 导入每个 excel 文件,2) 重命名具有特定名称的列(所有 65 个文件应具有相同的列名),3) 使用任何函数(例如 rbind())合并 65 个文件) 到一个文件中,以及 4) 用今天的日期命名它。
我用过:
mydir = setwd("~/Dropbox/JanuaryFiles")
myfiles = list.files(path=mydir, pattern="*.xlsx", full.names=TRUE)
myfiles
# Here: I need to rename the columns of each excel file:
# Then I used the following to combine the files:
combined.df <- do.call(cbind , dat_All)
# I'm also still missing the function that names the final file with specific name and today's date.
您可以在 sapply
循环中使用 openxlsx::read.xlsx
打开文件并使用 setNames
.
设置列名
col.names <- paste0("V", 1:4) ## new column names
file.names <- sapply(1:3, function(i) paste0("file", i, ".xlsx")) ## your file names
library(openxlsx)
r <- do.call(rbind, lapply(file.names, function(x) setNames(read.xlsx(x), col.names)))
r
# V1 V2 V3 V4
# 1 1 4 7 10
# 2 2 5 8 11
# 3 3 6 9 12
# 4 1 4 7 10
# 5 2 5 8 11
# 6 3 6 9 12
# 7 1 4 7 10
# 8 2 5 8 11
# 9 3 6 9 12
数据:
dat <- data.frame(matrix(1:12, 3, 4))
sapply(1:3, function(i) openxlsx::write.xlsx(dat, paste0("file", i, ".xlsx")))
lapply
函数会将您的所有数据帧读入名为 dfs
的列表 object 中。 col_names
参数是您提供列名称的地方。 skip
是忽略第一行,它有错误的 header 名称(如果要包含 excel 文件的第一行,请删除它)。
dplyr::bind_rows
会将数据帧列表堆叠成一个小标题 object.
sprintf("%s.xlsx", Sys.Date())
使用今天的日期创建文件名。您可以使用 format
函数修改输出格式(例如 format(Sys.Date(), "%m-%d-%Y"
)。然后 xlsx::write.xlsx
输出数据帧。注意:它必须是数据帧而不是小标题 object 这就是我使用 as.data.frame
.
的原因
library(dplyr)
library(xlsx)
library(readxl)
# Provide a character vector of column names you want to col_names
dfs <- lapply(myfiles, readxl::read_excel, col_names = cols, skip = 1)
df <- dplyr::bind_rows(dfs)
xlsx::write.xlsx(as.data.frame(df), sprintf("%s.xlsx", Sys.Date()))
我有 65 excel 个文件存储在一个文件夹中(即 JanuaryFiles)。我需要:
- 导入每个 excel 文件,2) 重命名具有特定名称的列(所有 65 个文件应具有相同的列名),3) 使用任何函数(例如 rbind())合并 65 个文件) 到一个文件中,以及 4) 用今天的日期命名它。
我用过:
mydir = setwd("~/Dropbox/JanuaryFiles")
myfiles = list.files(path=mydir, pattern="*.xlsx", full.names=TRUE)
myfiles
# Here: I need to rename the columns of each excel file:
# Then I used the following to combine the files:
combined.df <- do.call(cbind , dat_All)
# I'm also still missing the function that names the final file with specific name and today's date.
您可以在 sapply
循环中使用 openxlsx::read.xlsx
打开文件并使用 setNames
.
col.names <- paste0("V", 1:4) ## new column names
file.names <- sapply(1:3, function(i) paste0("file", i, ".xlsx")) ## your file names
library(openxlsx)
r <- do.call(rbind, lapply(file.names, function(x) setNames(read.xlsx(x), col.names)))
r
# V1 V2 V3 V4
# 1 1 4 7 10
# 2 2 5 8 11
# 3 3 6 9 12
# 4 1 4 7 10
# 5 2 5 8 11
# 6 3 6 9 12
# 7 1 4 7 10
# 8 2 5 8 11
# 9 3 6 9 12
数据:
dat <- data.frame(matrix(1:12, 3, 4))
sapply(1:3, function(i) openxlsx::write.xlsx(dat, paste0("file", i, ".xlsx")))
lapply
函数会将您的所有数据帧读入名为 dfs
的列表 object 中。 col_names
参数是您提供列名称的地方。 skip
是忽略第一行,它有错误的 header 名称(如果要包含 excel 文件的第一行,请删除它)。
dplyr::bind_rows
会将数据帧列表堆叠成一个小标题 object.
sprintf("%s.xlsx", Sys.Date())
使用今天的日期创建文件名。您可以使用 format
函数修改输出格式(例如 format(Sys.Date(), "%m-%d-%Y"
)。然后 xlsx::write.xlsx
输出数据帧。注意:它必须是数据帧而不是小标题 object 这就是我使用 as.data.frame
.
library(dplyr)
library(xlsx)
library(readxl)
# Provide a character vector of column names you want to col_names
dfs <- lapply(myfiles, readxl::read_excel, col_names = cols, skip = 1)
df <- dplyr::bind_rows(dfs)
xlsx::write.xlsx(as.data.frame(df), sprintf("%s.xlsx", Sys.Date()))