简体   繁体   中英

R data frame: Transform absolute date into day 1 for multiple factors

I have a data frame which is organized as follows:

Subject ID  |      Collected Date        | Value  
3933332            9/14/2004 9:38           0.576
3933332            9/15/2004 3:48           1.164
3933332            9/17/2004 9:38           2.112
3933332            9/21/2004 1:00           1.547
6955455            5/12/2011 2:11           0.012
6955455            5/13/2011 1:31           0.023
6955455            5/14/2011 3:11           0.112
6955455            5/17/2011 12:11          0.342
...                ....                     ...

What I need is:

Subject ID  |      Day                   | Value  
3933332            1                        0.576
3933332            2                        1.164
3933332            4                        2.112
3933332            8                        1.547
6955455            1                        0.012
6955455            2                        0.023
6955455            3                        0.112
6955455            6                        0.342
...                ....                     ...

edit: for clarification, the new subject ID has a new start date.

I know that I can use as.Date to format the date (time is not relevant for me). Next step would probably be to use a plyr and/or apply function to split up the set by subject ID, but I don't know how to do that.

Can you help me?

Here is one idea. I am not sure how exactly your data was composed. I, therefore, decided to create a data frame based on what you have above. Please note that Date is character in foo .

library(dplyr)

mutate(group_by(foo, ID),
       Date = as.Date(Date, format = "%m/%d/%Y %H:%M"), 
       nDay = as.character(Date - Date[1] + 1))

#       ID       Date value nDay
#1 3933332 2004-09-14 0.576    1
#2 3933332 2004-09-15 1.164    2
#3 3933332 2004-09-17 2.112    4
#4 3933332 2004-09-21 1.547    8
#5 6955455 2011-05-12 0.012    1
#6 6955455 2011-05-13 0.023    2
#7 6955455 2011-05-14 0.112    3
#8 6955455 2011-05-17 0.342    6

Extra: This is my naive translation of the code above to a data.table idea.

DT = setDT(foo)[, Date := as.Date(Date, format = "%m/%d/%Y %H:%M")] 
DT[, nDay := as.character(Date - Date[1] + 1), by = ID]

DATA

foo <- structure(list(ID = c(3933332, 3933332, 3933332, 3933332, 6955455, 
6955455, 6955455, 6955455), Date = c("9/14/2004 9:38", "9/15/2004 3:48", 
"9/17/2004 9:38", "9/21/2004 1:00", "5/12/2011 2:11", "5/13/2011 1:31", 
"5/14/2011 3:11", "5/17/2011 12:11"), value = c(0.576, 1.164, 
2.112, 1.547, 0.012, 0.023, 0.112, 0.342)), .Names = c("ID", 
"Date", "value"), row.names = c(NA, -8L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM