0

I have a data frame that looks like the following:

Day Minutes Status
1 0 Play
1 10 Eat
1 30 Move
1 50 Transport
2 0 Play
2 20 Transport
2 50 Sleep

Is it possible to pivot the table to have my Day as an index while the column names are the status and the values are the minutes?

Desired Output:

Day Play Eat Move Transport Play Transport Sleep
1
2
Jordan Iatro
  • 289
  • 3
  • 10

1 Answers1

0

You can use pivot_wider from tidyr (part of the tidyverse). You can supply the new column names using names_from, then you want to fill in the values with the data from Minutes.

library(tidyverse)

df %>% 
  pivot_wider(names_from = "Status", values_from = "Minutes")

Output

    Day  Play   Eat  Move Transport Sleep
  <int> <int> <int> <int>     <int> <int>
1     1     0    10    30        50    NA
2     2     0    NA    NA        20    50

Data

df <- structure(list(Day = c(1L, 1L, 1L, 1L, 2L, 2L, 2L), Minutes = c(0L, 
10L, 30L, 50L, 0L, 20L, 50L), Status = c("Play", "Eat", "Move", 
"Transport", "Play", "Transport", "Sleep")), class = "data.frame", row.names = c(NA, 
-7L))
AndrewGB
  • 16,126
  • 5
  • 18
  • 49