【问题标题】:How to align Dates from different columns in R or Excel如何对齐 R 或 Excel 中不同列的日期
【发布时间】:2019-02-06 14:24:26
【问题描述】:

我有一个变量数据集,每个变量都有不同的日期跨度。它们如以下示例所示(从 500 个案例中抽取前两个案例):

DatesV1     DatesV2
29/12/1995  19/07/2001
02/01/1996  20/07/2001
03/01/1996  23/07/2001
04/01/1996  24/07/2001
05/01/1996  25/07/2001
08/01/1996  26/07/2001
09/01/1996  27/07/2001
10/01/1996  30/07/2001
11/01/1996  31/07/2001
12/01/1996  01/08/2001
15/01/1996  02/08/2001
16/01/1996  03/08/2001
17/01/1996  06/08/2001

我希望DatesV2 中的日期与DatesV1 中的日期一致。这意味着DatesV2 将从几个NA 开始,直到日期对齐的行。像这样:

DatesV1     DatesV2
 ...         ...
17/07/2001  NA
18/07/2001  NA
19/07/2001  19/07/2001
20/07/2001  20/07/2001
 ...         ...

在示例集中,我有一个正是我想要做的示例。对于我拥有的 500 个变量,我找不到在 R 或 Excel 中执行此操作的快速计算方法。 Example Set 我尝试过这样的事情:

nhat<-which(Example$DatesV2[1]==Example$DatesV1)
nend<-which(Example$DatesV1[length(Example$DatesV1)-1]==Example$DatesV2)
Example$Apotelesma<- c(rep(NA,nhat-1),Example$DatesV2[1:nend],NA)

这是一个适用于两个日期的初始测试。唯一的问题是日期显示为数字。

【问题讨论】:

    标签: r excel date


    【解决方案1】:

    这是一个使用一些重塑的可能解决方案。我用一个简单的例子:

    df = data.frame(DatesV1 = c("24/07/2001","25/07/2001","26/07/2001"),
                    DatesV2 = c("25/07/2001","26/07/2001","27/07/2001"),
                    DatesV3 = c("26/07/2001","27/07/2001","28/07/2001"),
                    stringsAsFactors = F)
    
    library(tidyverse)
    library(lubridate)
    
    # update to date columns (only if needed)
    df = df %>% mutate_all(dmy)
    
    df %>%
      gather() %>%             # reshape dataset
      mutate(id = value) %>%   # use date values as row ids
      spread(key, value) %>%   # reshape again
      select(-id)              # remove ids
    
    #      DatesV1    DatesV2    DatesV3
    # 1 2001-07-24       <NA>       <NA>
    # 2 2001-07-25 2001-07-25       <NA>
    # 3 2001-07-26 2001-07-26 2001-07-26
    # 4       <NA> 2001-07-27 2001-07-27
    # 5       <NA>       <NA> 2001-07-28
    

    【讨论】:

    • 我明白了:df = df %&gt;% mutate_all(dmy) Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found. 另外:class(df$DatesV1) [1] "Date"
    • dmy(df$DatesV2) 为所有条目返回 NA
    • 我假设您设法运行了我的示例,但是您得到了完整的数据集,对吧?要么你有其他不是日期的变量,要么你有另一种日期格式。
    • 是的!您的示例运行完美!就在我展开我的案例时,错误出现了。
    • 如果您的列已经是date,也许您不需要那段代码。它们有相同的格式吗? dmy 是“日-月-年”。
    【解决方案2】:

    如果您愿意,这是一种丑陋/凌乱的方法,但它可以完成工作。任何更快更整洁的方式都会更好。

    n<-nrow(DataAlignment)
    Newdata<-matrix(0,5148,ncol(DataAlignment))
    loops<-ncol(DataAlignment)-1
    for(i in 1:loops){
      nhat<-which(DataAlignment[1,i+1]==DataAlignment[,1]) #finds the position of the first date in column 2 according to the first column
      nend<-which(DataAlignment[n,1]==DataAlignment[,i+1]) #finds the position of last date in col 2 according to the first column
    
      if(nhat==1 | nend != 5148){ #takes into account when they start at the same time but end in different dates
      Newdata[,i+1]<-c(DataAlignment[c(1:nend),i+1],rep(NA,n-nend))  
      }
      else{if(nhat==1| nend==5148){Newdata[,i+1]<-c(DataAlignment[,i+1])} #this takes account when they start and end at the same time
      else{if(nhat!=1){
      Newdata[,i+1]<-c(rep(NA,nhat-1),DataAlignment[c(1:nend),i+1])}}} #creates the new data
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 2023-01-13
      • 2020-01-04
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多