Having trouble with pivot_longer

Hello guys

I have the following dataframe

median_time <- c("4.5", "141.2","33.1","48.3") %>% as.numeric()
second_date_picked <- c("first_canceled_time", "first_in_production_time", "first_canceled_time","first_in_production_time") %>% as.factor()
date <- c("2020-02-22", "2020-02-22", "2020-02-23", "2020-02-23") %>% date()

teste <- data.frame(date, second_date_picked, median_time) %>% as_tibble()

teste %>% pivot_longer(second_date_picked:median_time)

and I basically want to have "first_canceled_time" and "first_in_production_time" as a column.
I am getting the error: "Error: Can't combine second_date_picked <factor> and median_time ."

So I would like something like this

date first_canceled_time first_in_production_time
2020-02-22. 4.5 141
2020-02-23. 33.1 48.3

What Am I doing wrong here?

You want pivot_wider rather than pivot_longer:

teste %>% pivot_wider(names_from = second_date_picked,
                      values_from = median_time)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.