R 代码错误地将日期格式写入 excel

R code incorrectly writing date format to excel

我正在使用 R 分析一些数据,但在获取我的输出文件(excel 工作簿)以正确显示 Aus 格式的日期 (DD/MM/YYYY) 时遇到了问题。输入文件是带有站点和日期的 csv。

我检查过输入文件中的 excel 日期都是带有 *(适应本地时间格式)和没有 * 的澳大利亚格式。我什至尝试在美国制作 excel 格式 date/time 认为这至少应该提供正确的输出,即使它的顺序有点错误。

#Coerce variables into useful types
Treated_LORs$DATE <- as.Date(Treated_LORs$DATE)
Treated_LORs[, 18:44] <- sapply(Treated_LORs[, 18:44], function(x)     as.numeric(x))

#Remove empty rows if any
Treated_LORs <- Treated_LORs[apply(Treated_LORs, 1, function(x) any(!is.na(x))),]

#Create a list of split data frames, split by site and sampling year
split_Treated_LORs <- split(Treated_LORs, f=list(Treated_LORs$SITENAME, Treated_LORs$Sampling.Year), drop=TRUE)

#Run the [calculate_Daily_Average_RA_PAF_Categories_Option2] function     from the sourced R script above
for(i in 1:length(split_Treated_LORs)){
  calculate_Daily_Average_RA_PAF_Categories_Option2(split_Treated_LORs[[i]])
}

我希望输出为澳大利亚日期格式 (DD/MM/YYYY),但它看起来要换成美国格式,例如11/10/2015(2015 年 10 月 11 日)变为 0011-10-20 看起来它正在将输入日期读取为 YYYY/MM/DD。请帮忙!

我不确定我是否理解这个问题,但要格式化日期,您可以尝试 lubridate 中的 dmy(日月年)函数,或将格式指定为 as.Date

library(lubridate)

lubridate::dmy("11/10/2015")

as.Date("11/10/2015", format = "%d/%m/%Y")

查看 ?strptime 以获取有关其他格式的信息。

您还可以输出特定格式的日期

x <- as.Date('2011-10-11')
format(x, format = '%d/%m/%Y')
[1] "11/10/2011"