so I currently have a dataset of user_ids and medication type, I am trying to format the set so all of the medication to columns and if the user takes them it will have a 1 and if not a 0. Im not sure the best way to implement this i have tried spread()
but it doesnt like the dataset due to the user_ids not being unique.
This is what I am trying to create:
#starting df
df1 <- data.frame (user_id =c("1", "1", "2", "3","3"),
medication_name =c("Carbamzepine", "Clonazepam", "Lamotrigine", "Zonisamide", "Gabapentin"))
#The df im trying to make
df2 <- data.frame(user_id = c("1", "2", "3"),
Carbamzepine = c("1","0","0"),
Clonazepam = c("1", "0","0"),
Lamotrigine = c("0","1","0"),
Zonisamide = c("0", "0", "1"),
Gabapentin = c("0", "0", "1"))