I have a dataframe with rows such as:
dt <- data.frame(id = c(1,1), start = as.Date(c("2020-01-01", "2021-02-01")), end = as.Date(c("2020-12-31", "2022-01-31")), cancelled = as.Date(c("2021-01-10", NA)))
dt
## id start end cancelled
## 1 2020-01-01 2020-12-31 2021-01-10
## 1 2021-02-01 2022-01-31 <NA>
and I would like to pivot_longer to obtain:
id date action
1 2020-01-01 start
1 2020-12-31 end
1 2021-01-10 cancelled
1 2021-02-01 start
1 2022-01-31 end
I thought that this would work, but it produces an error:
dt %>%
pivot_wider(
cols = !id,
names_to = "date",
values_to = "action")
Error: 2 components of `...` were not used.
We detected these problematic arguments:
* `names_to`
* `values_to`
Did you misspecify an argument?
colsas-idinstead of!idpivot_widerinstead ofpivot_longer. Usepivot_longer(dt, cols = !id, names_to = "date", values_to = "action", values_drop_na = TRUE)