After using pivot_wider(), I have a data frame like this:
| country | year | variable 1 | variable 2 |
|---|---|---|---|
| A | 2000 | 0.5 | NA |
| A | 2000 | NA | 68 |
| B | 2000 | NA | 55 |
| B | 2000 | 0.9 | NA |
What is the easiest way to make it like this:
| country | year | variable 1 | variable 2 |
|---|---|---|---|
| A | 2000 | 0.5 | 68 |
| B | 2000 | 0.9 | 55 |
Thank you
Before pivot_wider(), the data frame was like this:
| country | year | variables | values |
|---|---|---|---|
| A | 2000 | variable 1 | 0.5 |
| A | 2000 | variable 2 | 68 |
| B | 2000 | variable 1 | 0.9 |
| B | 2000 | variable 2 | 55 |
Code:
data <- data %>% pivot_wider(names_from = variables, values_from = values)
pivot_longer, that suggests the pivot was either done incorrectly or should not have been done in the first place. Please post the pre-pivot data and the code you used to get to this.id_cols