我有一个dateofbirth列,其中包含字符串类型的date,比如11-12-89,我想将它转换为date类型,以便使用lubridate:: year ()来提取年份。我想知道如何将其转换为日期格式,如11/12/1989 )。顺便说一下,当使用fwrite()输出csv文件并读取excel时,显示的出生日期类似于"11/12/1989",但在r中,当使用fread读取原始文件时,出生日期类似于"11-12-89“。
提前谢谢你。
发布于 2020-03-10 12:25:39
假设您的日期格式是日期-月-年。
在base R中,您可以使用:
#Convert to date
df$dateofbirth <- as.Date(df$dateofbirth, '%d-%m-%y')
#Get the year
df$year <- as.integer(format(df$dateofbirth, "%Y"))或使用lubridate
library(lubridate)
df$dateofbirth <- dmy(df$dateofbirth)
df$year <- year(df$dateofbirth)https://stackoverflow.com/questions/60611514
复制相似问题