1

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"))
zx8754
  • 52,746
  • 12
  • 114
  • 209

2 Answers2

2

Use table:

table(df1)

giving:

       medication_name
user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
      1            1          1          0           0          0
      2            0          0          0           1          0
      3            0          0          1           0          1

That gives a table object but if it is important that it be a data frame then convert it to a data frame and then move the row names to a user_id column.

library(tibble)
rownames_to_column(as.data.frame.matrix(table(df1)), "user_id")

giving:

  user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
1       1            1          1          0           0          0
2       2            0          0          0           1          0
3       3            0          0          1           0          1
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
1

You can use the following solution:

library(dplyr)
library(tidyr)

df %>%
  mutate(id = 1) %>%
  pivot_wider(names_from = medication_name, values_from = id, 
              values_fill = 0)


# A tibble: 3 x 6
  user_id Carbamzepine Clonazepam Lamotrigine Zonisamide Gabapentin
  <chr>          <dbl>      <dbl>       <dbl>      <dbl>      <dbl>
1 1                  1          1           0          0          0
2 2                  0          0           1          0          0
3 3                  0          0           0          1          1
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41