Reputation: 1642
I have the following data frame:
id Date Observation
1 2 A
1 3 B
1 8 C
2 11 C
2 3 B
2 7 A
3 9 A
3 10 A
3 5 B
I would like to organize the data into having multiple columns for each observation, the date to be chronologically ordered and each observation to get 0 before it appeared, 1 when it first appeared, and 1 afterward. In the end, it should look something like this:
id Date A B C
1 2 1 0 0
1 3 1 1 0
1 8 1 1 1
2 3 0 1 0
2 7 1 1 0
2 11 1 1 1
3 5 0 1 0
3 9 1 1 0
3 10 1 1 0
Upvotes: 0
Views: 30
Reputation: 388817
Reshape the data to wide format and for each id
turn all the value to 1 once it occurs.
library(dplyr)
library(tidyr)
df %>%
pivot_wider(names_from = Observation, values_from = Observation,
values_fn = length, values_fill = 0) %>%
arrange(id, Date) %>%
group_by(id) %>%
mutate(across(A:C, cummax)) %>%
ungroup
# id Date A B C
# <int> <int> <int> <int> <int>
#1 1 2 1 0 0
#2 1 3 1 1 0
#3 1 8 1 1 1
#4 2 3 0 1 0
#5 2 7 1 1 0
#6 2 11 1 1 1
#7 3 5 0 1 0
#8 3 9 1 1 0
#9 3 10 1 1 0
Upvotes: 1