Reputation: 43
I am trying to use lubridate to create separate 'start dates' for each participant. then I want lubridate to automatically generate 1 week intervals that correspond to the 'Week number' col.
What I'm trying to do:
while (week number = 1) set data$time to 1 while (week number = 2) set data$time to 2 ... while (week number = 25) set date$time to 25
create interval of 7 days from time 1, so that each participant has 25 rows (1 for each week). If a participant skipped week 3, for example, they would still have a week 3 row, but the values would be empty.
while (mydata$Weeknum == "Week1") {
mydata$time1 <- mydata$RecorededDate
}
int <- int.start(mydata$start_time, 7days)
In this code the while loop is bugging out and after that I am stuck as to what to do to match the interval to the week number. Thanks for your help in advance.
structure(list(Weeknum = c("Week1", "Week1", "Week1", "Week1",
"Week1", "Week1"), V1 = structure(c(NA, 1544891009, NA, NA, NA,
NA), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-6L), .internal.selfref = <pointer: 0x10380f2e0>, class = c("data.table",
"data.frame"))
Upvotes: 1
Views: 107
Reputation: 389315
You can group by each participant
and take difference of RecorededDate
with the first RecorededDate
to count number of weeks.
library(dplyr)
result <- mydata %>%
group_by(participant) %>%
mutate(time1 = ceiling(as.numeric(difftime(RecorededDate,
na.omit(RecorededDate)[1], units = 'week'))))
result
RecorededDate
column should be of class Date.
Upvotes: 0
Reputation: 174546
You don't need loops or lubridate:
seq(as.Date("2020-01-01"), by = "week", length = 10)
#> [1] "2020-01-01" "2020-01-08" "2020-01-15" "2020-01-22" "2020-01-29"
#> [6] "2020-02-05" "2020-02-12" "2020-02-19" "2020-02-26" "2020-03-04"
If you have a number of different start dates, and you want to make a single long vector with 25 weeks from each start date, you can do:
# Example start dates
start_dates <- as.Date(c("2020-01-01", "2020-03-04", "2019-03-02"))
do.call(c, lapply(start_dates, seq, by = "week", length.out = 25))
#> [1] "2020-01-01" "2020-01-08" "2020-01-15" "2020-01-22" "2020-01-29"
#> [6] "2020-02-05" "2020-02-12" "2020-02-19" "2020-02-26" "2020-03-04"
#> [11] "2020-03-11" "2020-03-18" "2020-03-25" "2020-04-01" "2020-04-08"
#> [16] "2020-04-15" "2020-04-22" "2020-04-29" "2020-05-06" "2020-05-13"
#> [21] "2020-05-20" "2020-05-27" "2020-06-03" "2020-06-10" "2020-06-17"
#> [26] "2020-03-04" "2020-03-11" "2020-03-18" "2020-03-25" "2020-04-01"
#> [31] "2020-04-08" "2020-04-15" "2020-04-22" "2020-04-29" "2020-05-06"
#> [36] "2020-05-13" "2020-05-20" "2020-05-27" "2020-06-03" "2020-06-10"
#> [41] "2020-06-17" "2020-06-24" "2020-07-01" "2020-07-08" "2020-07-15"
#> [46] "2020-07-22" "2020-07-29" "2020-08-05" "2020-08-12" "2020-08-19"
#> [51] "2019-03-02" "2019-03-09" "2019-03-16" "2019-03-23" "2019-03-30"
#> [56] "2019-04-06" "2019-04-13" "2019-04-20" "2019-04-27" "2019-05-04"
#> [61] "2019-05-11" "2019-05-18" "2019-05-25" "2019-06-01" "2019-06-08"
#> [66] "2019-06-15" "2019-06-22" "2019-06-29" "2019-07-06" "2019-07-13"
#> [71] "2019-07-20" "2019-07-27" "2019-08-03" "2019-08-10" "2019-08-17"
Upvotes: 2