问题描述
我正在尝试将Excel电子表格导入到R中(通过read.xlsx2()
)。Excel数据有一个日期列。该日期列包含混合类型的日期格式,例如,有些行为42669
,有些行为日期格式,例如26/10/2016
。
read.xlsx2()
将其作为一个因子读取,因此我使用下面的代码将其转换为as.Date。这适用于所有数字形式的日期(例如42669
),但R警告我它添加了一些NA
s(对于26/10/2016
格式的日期))。我的问题是,如何为所有变量导入具有适当日期的EXCEL数据,即告诉R存在混合数据?
library(xlsx)
#Import excel file
df <- read.xlsx2(mydata, 1, header=true)
#Output = recd_date : Factor w/ 590 levels "", "26/10/2016", "42669" ...
levels(df$recd_date)
#Output = [1] "" "26/10/2016" "42669" ...
#This works for numeric dates:
df$recd_date <- as.Date( as.numeric (as.character(df$recd_date) ),origin="1899-12-30")
#Output = recd_date : Date, format "2016-10-26" ...
#but it doesn't work for dd/mm/yyyy dates, R just replaces these with NA
推荐答案
如果需要,我们可以应用一个函数来清除日期,基本上是这样的:
cleanDate <- function(x) {
if (all(nchar(df2$date.mix) < 10)) {
cd <- as.Date(x)
} else {
cd <- do.call(c,
lapply(x, function(i)
if (nchar(i) < 10)
as.Date(as.numeric(i), origin="1970-01-01")
else as.Date(i)))
}
return(cd)
}
示例
# generate test df
df1 <- data.frame(date.chr=as.character(as.Date(1:3, origin=Sys.Date())),
date.num=as.numeric(as.Date(1:3, origin=Sys.Date())),
date.mix=as.character(as.Date(1:3, origin=Sys.Date())),
stringsAsFactors=FALSE)
df1[2, 3] <- as.character(as.numeric(as.Date(df1[2, 1])))
> df1
date.chr date.num date.mix
1 2019-02-01 17928 2019-02-01
2 2019-02-02 17929 17929
3 2019-02-03 17930 2019-02-03
# write it to working directory
library(xlsx)
write.xlsx2(df1, "df1.xlsx")
# read it
# we use opt. `stringsAsFactors=FALSE` to prevent generation of factors
df2 <- read.xlsx2("df1.xlsx", 1, stringsAsFactors=FALSE)
> df2
X. date.chr date.num date.mix
1 1 2019-02-01 17928 2019-02-01
2 2 2019-02-02 17929 17929
3 3 2019-02-03 17930 2019-02-03
现在我们使用lapply()
应用该函数。
date.cols <- c("date.chr", "date.num", "date.mix") # select date columns
df2[date.cols] <- lapply(df2[date.cols], cleanDate)
结果
> df2
X. date.chr date.num date.mix
1 1 2019-02-01 2019-02-01 2019-02-01
2 2 2019-02-02 2019-02-02 2019-02-02
3 3 2019-02-03 2019-02-03 2019-02-03