Here's an example of a data frame I am using.
ID <- c(1,1,2,3)
Type <- c('A', 'B', 'A', 'A')
Value <- c(2.5, 8, 10, 7)
df <- data.frame(ID,Type,Value)
df
And here's how df looks like:
| ID | Type | Value |
|---|---|---|
| 1 | A | 2.5 |
| 1 | B | 8.0 |
| 2 | A | 10.0 |
| 3 | A | 7.0 |
I'm looking for a way to transpose df in a way that the elements of the character variable "Type" become columns in the new data frame.
| ID | A | B |
|---|---|---|
| 1 | 2.5 | 8 |
| 2 | 10.0 | 0 |
| 3 | 7.0 | 0 |
Note that for IDs 2 and 3, where only type A values are present in df, I'd like to have zero values under column B in the new data frame.