格式现有的日期列不同的格式中的R
问题描述:
我需要日期列转换为不同的格式R.格式现有的日期列不同的格式中的R
数据:
Year Plant_Date
2010 May-08-2010
2010 Apr-09-2010
2010 June-02-2010
输出:
Year Plant_Date
2010 05/08/2010
2010 04/09/2010
2010 06/02/2010
怎么办这在R?
答
查看as.Date
函数的文档,然后在日期对象上使用format
函数(例如%Y是4位年份,%d是日期,%b是月份缩写)。我假设“六月”是“君”,因为其他月份是缩写:
# the raw strings:
> (inString <- c("May-08-2010", "Apr-09-2010", "Jun-02-2010"))
[1] "May-08-2010" "Apr-09-2010" "Jun-02-2010"
# convert to date object:
> (inDates <- as.Date(inDates, format = "%b-%d-%Y"))
[1] "2010-05-08" "2010-04-09" "2010-06-02"
# format using format function:
> format(inDates, "%m/%d/%Y")
[1] "05/08/2010" "04/09/2010" "06/02/2010"