0

I have a data.frame where I need to extract values into their own variables (columns), but keep the integrity of the unique identifier, so that there is only one unique ID per observation (row). I have tried using group_by(), mutate, and spread to no avail. I have tried using dcast and melt but must be missing something. I have also tried solutions posted here, here, and here, but none of them seem to work for my situation.

My data.frame looks like this;

UID<-c(rep(1:25, 2), rep(26:50, 2))
Group<-c(rep(5, 25), rep(20, 25), rep(-18, 25), rep(-80, 25))
Value<-sample(100:5000, 100, replace=TRUE)
df<-data.frame(UID, Group, Value)

Please help me figure out how extract these values to one unique ID per line (see expected output image arranged by UID here). If it is easier the empty values can be NULL, NA, or 0 (yes really actually zero).

Dustin
  • 183
  • 7
  • 1
    `tidyr::pivot_wider(df, names_from = Group, values_from = Value, values_fill = list(Value = 0))` ? – Ronak Shah Jul 07 '20 at 02:15
  • @RonakShah, Whoa, that is a really cool function that t worked perfectly!! If you post this as an answer I will mark it as working. I did have to update tidyverse then all my other packages in order for ```pivot_wider()``` to work. This worked on my real data, too! I did have to make a subset of just those three variables for it to work (on the larger dataset, it kept multiple unique identifiers). Thank you so much!!!! – Dustin Jul 07 '20 at 02:53
  • I'm surprised this has been closed as this answer is different, and my comment provides additional information to those with the same issue. I did not test the solution on the forwarding page, but if works the same, it should be fine. One interesting thing is that on the dummy data the missing values are 0's and in the real data they are NA, even though values are both integers. – Dustin Jul 07 '20 at 03:10

0 Answers0