1

My sample dataset has multiple columns that I want to convert into wide format. I have tried using the dcast function, but I get error. Below is my sample dataset:

df2 = data.frame(emp_id = c(rep(1,2), rep(2,4),rep(3,3)), 
                 Name = c(rep("John",2), rep("Kellie",4), rep("Steve",3)),
                 Year = c("2018","2019","2018","2018","2019","2019","2018","2019","2019"),
                 Type = c(rep("Salaried",2), rep("Hourly", 2), rep("Salaried",2),"Hourly",rep("Salaried",2)),
                 Dept = c("Sales","IT","Sales","Sales", rep("IT",3),rep("Sales",2)),
                 Salary = c(100,1000,95,95,1500,1500,90,1200,1200))

I'm expecting my output to look like:

enter image description here

hk2
  • 487
  • 3
  • 15

2 Answers2

2

One option is the function pivot_wider() from the tidyr package:

df.wide <- tidyr::pivot_wider(df2,
                              names_from = c("Type", "Dept", "Year"),
                              values_from = "Salary",
                              values_fn = {mean})

This should get you the desired result.

0

What do you think about this output? It is not the expected output, but somehow I find it easier to interpret the data??

df2 %>% 
    group_by(Name, Year, Type, Dept) %>% 
    summarise(mean = mean(Salary))

Output:

  Name   Year  Type     Dept   mean
  <chr>  <chr> <chr>    <chr> <dbl>
1 John   2018  Salaried Sales   100
2 John   2019  Salaried IT     1000
3 Kellie 2018  Hourly   Sales    95
4 Kellie 2019  Salaried IT     1500
5 Steve  2018  Hourly   IT       90
6 Steve  2019  Salaried Sales  1200
TarJae
  • 72,363
  • 6
  • 19
  • 66