I am working with a NHANES derived dataset on prescription drug and dietary use. Its drug level data needs to be converted to personal level data (i.e. a record for each person) before merging it with NHANES demographic data files by the unique identifier for each individual (variable called seqn).
seqn rxduse rxddrgid rxdcount
73557 1 d00262 2
73557 1 d04113 2
73557 1 d00262 4
73558 1 d04538 4
73558 1 d00746 4
73558 1 d03182 4
Tried this code
staxpiv <- stax22 %>%
group_by(SEQN) %>%
mutate(id = 1:n()) %>%
ungroup() %>%
pivot_wider(values_from = RXDUSE2,
names_from = RXDDRGID,
names_prefix = 'id_')
It didn't work, this was the output:
SEQN id id_ id_d03740 id_d04532 id_d00325
93704 1 0 NA NA
93705 1 NA 1 NA
93705 2 NA NA 1
Would greatly appreciate any assistance with this.