R 添加一个新列,其中月份从一个单独的已存在的“月份”中提取;日期“;(mdy)列
正在尝试在我的数据表中添加一个新列,该列表示月份(以数值或字符的形式),使用已可用的“SetDate”列,该列的格式为mdyR 添加一个新列,其中月份从一个单独的已存在的“月份”中提取;日期“;(mdy)列,r,date,datatable,lubridate,R,Date,Datatable,Lubridate,正在尝试在我的数据表中添加一个新列,该列表示月份(以数值或字符的形式),使用已可用的“SetDate”列,该列的格式为mdy 我是新手,遇到了麻烦。谢谢您base解决方案: f = "%m/%d/%y" # note the lowercase y; it's because the year is 92, not 1992 dataset$SetDateMonth <- format(as.POSIXct(dataset$SetDate, format = f), "%m") 试试这个
我是新手,遇到了麻烦。谢谢您
base
解决方案:
f = "%m/%d/%y" # note the lowercase y; it's because the year is 92, not 1992
dataset$SetDateMonth <- format(as.POSIXct(dataset$SetDate, format = f), "%m")
试试这个(创建一个小示例):
请不要使用代码的图片。以可复制的形式包含数据,例如使用
dput(数据)
并包含输出
format(as.POSIXct('1/1/92', format = "%m/%d/%y"), "%m")
[1] "01"
library(lubridate)
date_example <- "1/1/92"
lubridate::mdy(date_example)
[1] "1992-01-01"
lubridate::mdy(date_example) %>% lubridate::month()
[1] 1
lubridate::mdy(date_example) %>% lubridate::month(label = TRUE, abbr = FALSE)