Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 添加一个新列,其中月份从一个单独的已存在的“月份”中提取;日期“;(mdy)列_R_Date_Datatable_Lubridate - Fatal编程技术网

R 添加一个新列,其中月份从一个单独的已存在的“月份”中提取;日期“;(mdy)列

R 添加一个新列,其中月份从一个单独的已存在的“月份”中提取;日期“;(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") 试试这个

正在尝试在我的数据表中添加一个新列,该列表示月份(以数值或字符的形式),使用已可用的“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")
试试这个(创建一个小示例):


请不要使用代码的图片。以可复制的形式包含数据,例如使用
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)