【发布时间】:2020-11-04 23:22:38
【问题描述】:
所以我有一个 excel 文件,其中包含我必须合并的几张表中的数据,以便我可以从中提供见解:
这些表以从 11 月 .....10 月开始的每个月命名(共:12 张)
我的代码是这样开始的:
#List of months to look at
months = c("Novemeber", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September")
我想要做的是将这些工作表中的每一个的列名与一个空的 df 匹配(我称之为差异),并相应地将数据提取到这些列。我的代码是这样的
discrepancies <-
setNames(
data.frame(matrix(ncol = 12, nrow = 0)),
c(
"Date",
"Officer",
"Case Number",
"Account Number",
"Plan Type",
"Type",
"ID",
"Transaction Amount",
"Code",
"Specialist",
"Transit#",
"Processed Via"
)
)
#Query for each month's data and append to the main dataframe
for (i in months) {
temp <- read_excel(
"G:/Confidental.xlsx",
sheet = i,
col_names = TRUE,
skip = 0
)
temp$`months` <- i
discrepancies <- rbind(discrepancies, temp)
}
此代码将工作表中的每个字段与我想要的列进行比较,当一张表的列数与差异 df 中的列数不同时,它会卡住。任何帮助表示赞赏。
【问题讨论】:
-
每个 excel 表中的所有“差异”列总是存在,或者是否存在某些情况下它们可能会丢失?
-
后者,有些可能会丢失。但是不会有列在 12 列窗口之外
-
我对这个问题的回答可能会有用:stackoverflow.com/questions/64506027/…
标签: r