I have data that looks like this:
ID | Name | Adress | Score | Requirement | Status |
---|---|---|---|---|---|
1 | John | CA | 1 | Internet | OK |
1 | John | CA | 1 | TV | Not OK |
1 | John | CA | 1 | Household | OK |
2 | Ann | LA | 3 | Internet | Not OK |
2 | Ann | LA | 3 | TV | Follow up |
... | ... | ... | ... | ... | ... |
The ID is unique for each "customer", and from row 1-3 columns ID, Name, Adress and Score is the same. There are 3 requirements for each customer and I want to transform the 3 requirements as columns with the status as values, so that it deletes duplicates. As follows:
ID | Name | Adress | Score | Internet | TV | Household |
---|---|---|---|---|---|---|
1 | John | CA | 1 | OK | Not OK | OK |
2 | Ann | LA | 3 | Not OK | Follow up | OK |
... | ... | ... | ... | ... | ... | ... |
I have tried to search for previous cases with reshape and dcast in R studio, but have not found similar to mine. Can someone help me? Many thanks in advance!
you can use pivot_wider from tidyr package
library(dplyr)
library(tidyr)
# your data
df <- tribble(
~ID, ~Name, ~Adress, ~Score, ~Requirement, ~Status,
1, "John", "CA", 1, "Internet", "OK",
1, "John", "CA", 1, "TV", "Not OK",
1, "John", "CA", 1, "Household", "OK",
2, "Ann", "LA", 3, "Internet", "Not OK",
2, "Ann", "LA", 3, "TV", "Follow up")
df <- df %>%
pivot_wider(names_from = Requirement, values_from = Status)