标题名称为r中的日期

时间:2014-08-26 00:50:36

标签: r time-series

我试图计算"死亡"用户,这意味着我想确定用户注册程序与程序中不再处于活动状态之间的持续时间。我使用read.csv("filename",header=TRUE)读了两个文件:

 >   df
      name   start.date
1  Allison   2013-03-16
2   Andrew   2013-03-16
3     Carl   2013-03-16
4     Dora   2013-03-17
5   Hilary   2013-03-17
6    Louis   2013-03-19
7     Mary   2013-03-20
8   Mickey   2013-03-20

文件2:

> df2
       names X04.16.2013 X04.17.2013 X04.18.2014  X04.19.2013
2001 Allison           5           5           0           0
2002  Andrew           0           0           0           0
2003    Carl           8           8           11          10
2004    Dora           6           4           9           3
2005  Hilary           2           0           0           0
2006   Louis           18         10           8           3
2007    Mary           4           7           7           0
2008  Mickey           9           5           0           0

我想要做的是将df2的标题名称转换为日期,这样我就可以创建一个新的数据框,其中包含用户名,开始日期和"天死亡",其中当用户在df2中输入0时:

      name   start.date   days.to.death
1  Allison   2013-03-16   33
2   Andrew   2013-03-16   0
3     Carl   2013-03-16   NA
4     Dora   2013-03-17   NA
5   Hilary   2013-03-17   31
6    Louis   2013-03-19   NA
7     Mary   2013-03-20   30
8   Mickey   2013-03-20   28

请注意,安德鲁从未"活着"而卡尔,多拉和路易斯的避风港已经死了#34;然而。我还是R的新手,所以非常感谢任何输入!

3 个答案:

答案 0 :(得分:0)

具有正确格式的简单as.Date会将列名称转换为日期。首先,数据的可复制形式

df<-structure(list(name = structure(1:8, .Label = c("Allison", "Andrew", 
"Carl", "Dora", "Hilary", "Louis", "Mary", "Mickey"), class = "factor"), 
    start.date = structure(c(15780, 15780, 15780, 15781, 15781, 
    15783, 15784, 15784), class = "Date")), .Names = c("name", 
"start.date"), row.names = c("1", "2", "3", "4", "5", "6", "7", 
"8"), class = "data.frame")

df2<-structure(list(names = structure(1:8, .Label = c("Allison", "Andrew", 
"Carl", "Dora", "Hilary", "Louis", "Mary", "Mickey"), class = "factor"), 
    X04.16.2013 = c(5L, 0L, 8L, 6L, 2L, 18L, 4L, 9L), X04.17.2013 = c(5L, 
    0L, 8L, 4L, 0L, 10L, 7L, 5L), X04.18.2014 = c(0L, 0L, 11L, 
    9L, 0L, 8L, 7L, 0L), X04.19.2013 = c(0L, 0L, 10L, 3L, 0L, 
    3L, 0L, 0L)), .Names = c("names", "X04.16.2013", "X04.17.2013", 
"X04.18.2014", "X04.19.2013"), class = "data.frame", row.names = c("2001", 
"2002", "2003", "2004", "2005", "2006", "2007", "2008"))

现在

nn <- names(df2)[-1]
dts <- as.Date(nn, format="X%m.%d.%Y")
dts
# [1] "2013-04-16" "2013-04-17" "2014-04-18" "2013-04-19"

然后

lastedateid<-apply(df2[,-1], 1, function(x) {i<-which(x==0); ifelse(length(i), head(i,1), NA)})
lastdate <- dts[lastedateid]
lastdate
# [1] "2014-04-18" "2013-04-16" NA           NA           "2013-04-17"
# [6] NA           "2013-04-19" "2014-04-18"

并且只要df$name==df2$names

transform(df, days.to.death=difftime(lastdate,start.date, unit="days"))

将给出以下假设start.date也是正确的Date

     name start.date days.to.death
1 Allison 2013-03-16      398 days
2  Andrew 2013-03-16       31 days
3    Carl 2013-03-16       NA days
4    Dora 2013-03-17       NA days
5  Hilary 2013-03-17       31 days
6   Louis 2013-03-19       NA days
7    Mary 2013-03-20       30 days
8  Mickey 2013-03-20      394 days

答案 1 :(得分:0)

假设df2的列标题中有拼写错误,使用dplyr和tidyr的以下解决方案可以帮助您完成大部分工作......

  library(tidyr)
  library(dplyr)

  colnames(df)<-c("names", "start") #  To join dfs, the first column header needs to be identical to df2
  df$start<-as.Date(df$start, format="%m/%d/%Y") #formatting date

以下通过长期形成数据,格式化日期(类似于MrFlick的建议),然后仅保留其中包含0的日期,对df2起作用。然后它采用第一个实例(即最早的日期,假设您的日期按照从左到右的列的时间顺序)。然后计算从该日期(结束日期)到开始日期df的日期差异。我使用的格式与MrFlick相同 - 但您可以简单地将差异计算为整数。

  df2 %>% 
  filter(X04.16.2013!=0) %>%   #removes Andrew who has 0 in first date col
  gather(key,value,2:5) %>%     
  mutate(date=as.Date(key, format="X%m.%d.%Y")) %>%
  left_join(df) %>%
  filter(value==0) %>%
  group_by(names) %>%
  filter(date == nth(date, 1)) %>% 
  select(names, start, date) %>%
  mutate (daydiff=difftime(date,start, unit="days"))

给出了这个......

    names      start       date daydiff
1  Hilary 2013-03-17 2013-04-17 31 days
2 Allison 2013-03-16 2013-04-18 33 days
3  Mickey 2013-03-20 2013-04-18 29 days
4    Mary 2013-03-20 2013-04-19 30 days

应该很容易放入NAs和那些从未生活过的人。也许这有点帮助?​​

答案 2 :(得分:0)

以下简单代码可能很有用:

names(df2)[1] = 'name'
merge(df, ddf2)
dfm$days.to.death = ifelse(dfm[,3]==0,0,ifelse(dfm[,4]==0,31, ifelse(dfm[,5]==0,33,ifelse(dfm[,6]==0,28,NA))))

dfm[,c(1,2,7)]
     name start.date days.to.death
1 Allison 2013-03-16            33
2  Andrew 2013-03-16             0
3    Carl 2013-03-16            NA
4    Dora 2013-03-17            NA
5  Hilary 2013-03-17            31
6   Louis 2013-03-19            NA
7    Mary 2013-03-20            28
8  Mickey 2013-03-20            33