Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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 如何将日期分为年、月和日列?日期列记录为“日期”;2012年1月3日;_R - Fatal编程技术网

R 如何将日期分为年、月和日列?日期列记录为“日期”;2012年1月3日;

R 如何将日期分为年、月和日列?日期列记录为“日期”;2012年1月3日;,r,R,我有一个包含1000多个条目的大数据文件,我想将日期分为年、月和日列。我尝试使用as.Date(x),它给了我一个错误。strtime(x,format=“%y-%m-%d”),返回MAs。有人来帮我。日期栏编码为“2012-1-03”这是否满足了您的需求 library(dplyr) df <- tibble(date = "2012-Jan-03") %>% separate(date, into =c("year","mon

我有一个包含1000多个条目的大数据文件,我想将日期分为年、月和日列。我尝试使用as.Date(x),它给了我一个错误。strtime(x,format=“%y-%m-%d”),返回MAs。有人来帮我。日期栏编码为“2012-1-03”

这是否满足了您的需求

library(dplyr)
df <- tibble(date = "2012-Jan-03") %>%
  separate(date, into =c("year","month","day"), sep ="-")
# A tibble: 1 x 3
  year  month day  
  <chr> <chr> <chr>
1 2012  Jan   03 

库(dplyr)
df%
分开(日期,分为=c(“年”、“月”、“日”),九月=“-”)
#一个tibble:1 x 3
年月日
1 2012年1月3日

使用df$col将日期首先转换为日期
date <- "2012-Jan-03"

df <- data.frame(
  year = format(as.Date(date, "%Y-%b-%d"), "%Y"),
  month = format(as.Date(date, "%Y-%b-%d"), "%b"),
  day = format(as.Date(date, "%Y-%b-%d"), "%d")
)

df
  year month day
1 2012   Jan  03