I have a data set that I've subset which looks like this:
Item | Code | Percentage |
---|---|---|
10000 | 123 | 0.2 |
10001 | 134 | 0.98 |
10001 | 152 | 0.02 |
10002 | 123 | 0.68 |
10003 | 123 | 1 |
10002 | 178 | 0.32 |
10004 | 189 | 1 |
I want to find a way to transpose in a way where I only retain unique values from column A, Column B is dispersed into different columns according to unique values and the Percentage populates in those values. Please see the example of the data I'm looking to finalize with:
Item | 123 | 134 | 152 | 178 | 189 |
---|---|---|---|---|---|
10000 | 0.2 | 0 | 0 | 0 | 0 |
10001 | 0 | 0.98 | 0.02 | 0 | 0 |
10002 | 0.68 | 0 | 0 | 0.3 | 0 |
10003 | 1 | 0 | 0 | 0 | 0 |
10004 | 0 | 0 | 0 | 0 | 1 |
I am currently using a format the follows this "skeleton":
df <-df %>%
group_by(Item) %>%
mutate(n = row_number()) %>%
spread(Code, Percentage)
Following this structure I still get values in Column A repeating (not unique). I did load library(plyr) library(dplyr) library(tidyr) in that order. Reason why I mentioned is I read somewhere if you switch the order it works, but ended up messing up the results.
If you need more information please let me know. Thank you!