【问题标题】:How can I separate date into year,month and day column? Date column is recorded as "2012-Jan-03" [duplicate]如何将日期分为年、月和日列?日期列记录为“2012-Jan-03”[重复]
【发布时间】:2020-07-24 08:16:20
【问题描述】:

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

【问题讨论】:

标签: r


【解决方案1】:

这能满足你的需求吗?

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 

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2021-12-17
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 2022-07-22
      相关资源
      最近更新 更多