【问题标题】:Modify the date in a data frame in R修改R中数据框中的日期
【发布时间】:2018-10-31 15:45:33
【问题描述】:

最近我偶然发现了一个问题。不幸的是,我的日期变量没有被统一记录。

我得到了一个类似如下所示的数据框

Variable1 <- c(10,20,30,40,50)
Variable2 <- c("a", "b", "c", "d", "d")
Date <- c("today 10:45", "yesterday 3:10", "28 october 2018 5:32", "28 october 2018 8:32", "27 october 2018 5:32")
df <- data.frame(Variable1, Variable2, Date)
df

为了我的使用,我只需要提取它的日期。因此,我想根据“日期”创建一个新变量。

Date 变量应该只包含日期。小时与我的目的无关,可以忽略。

我的目标是得到以下数据框:

Variable1 <- c(10,20,30,40,50)
Variable2 <- c("a", "b", "c", "d", "d")
Date <- c("31 october 2018", "30 october 2018", "28 october 2018", "28 october 2018", "27 october 2018")
df2 <- data.frame(Variable1, Variable2, Date)
df2

最好日期的值也应该是正确的格式(日期)。

提前谢谢你。

【问题讨论】:

    标签: r date dataframe


    【解决方案1】:
    df$NewDate[grepl("today",df$Date)]<-Sys.Date() # Convert today to date
    df$NewDate[grepl("yesterday",df$Date)]<-Sys.Date()-1  # covert yesterday to date
    df$NewDate[is.na(df$NewDate)]<-df$Date[is.na(df$NewDate)] %>% as.Date(format="%d %b %Y")  # Convert explicit dates to date format
    class(df$NewDate)<-"Date"  # Convert column to Date class
    
    df
      Variable1 Variable2                 Date    NewDate
    1        10         a          today 10:45 2018-10-31
    2        20         b       yesterday 3:10 2018-10-30
    3        30         c 28 october 2018 5:32 2018-10-28
    4        40         d 28 october 2018 8:32 2018-10-28
    5        50         d 27 october 2018 5:32 2018-10-27
    

    【讨论】:

    • 非常感谢。一开始我遇到了一些困难,因为我的 R-Studio 没有设置为英语。但是命令Sys.setlocale("LC_TIME", "English") 解决了这个问题
    【解决方案2】:
    tolower(                                               # not strictly necessary, but for consistency
      gsub("yesterday", format(Sys.Date()-1, "%d %B %Y"),  # convert *day to dates
           gsub("today", format(Sys.Date(), "%d %B %Y"),
                gsub("\\s*[0-9:]*$", "",                   # remove the times
                     c("today 10:45", "yesterday 3:10", "28 october 2018 5:32", "28 october 2018 8:32", "27 october 2018 5:32")))))
    # [1] "31 october 2018" "30 october 2018" "28 october 2018" "28 october 2018" "27 october 2018"
    

    【讨论】:

    • 我想我默认在任何地方都使用gsub ...这可以很容易地使用sub作为前两个gsubs。
    【解决方案3】:

    另一种解决方案,使用索引。

    Date <- c("today 10:45", "yesterday 3:10", "28 october 2018 5:32", "28 october 2018 8:32", "27 october 2018 5:32")
    
    Date <- sub("today", Sys.Date(), Date)
    Date <- sub("yesterday", Sys.Date() - 1, Date)
    i <- grep("[[:alpha:]]", Date)
    Date[i] <- format(as.POSIXct(Date[i], format = "%d %B %Y %H:%M"), format = "%d %B %Y")
    Date[-i] <- format(as.POSIXct(Date[-i]), format = "%d %B %Y")
    
    Date
    #[1] "31 October 2018" "30 October 2018" "28 October 2018"
    #[4] "28 October 2018" "27 October 2018"
    

    然后我注意到solution by user r2evans,它将所有内容都转换为小写。因此,如有必要,以

    结尾
    Date <- tolower(Date)
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2021-10-29
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多