0

I have a df that looks like following and I would like to transform it to the one on right. What should I do? how should I handle for value?

enter image description here

df<-structure(list(Class = c("A", "A", "B", "B", "B", "C", "C", "C"
), standard = c("A", "U", "B", "B", "U", "U", "C", "C"), Subject = c("Math", 
"ELA", "Math", "ELA", "PE", "ELA", "Art", "Math"), Score = c(95, 
80, 73, 82, 75, 80, 88, 82)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))
Stataq
  • 2,237
  • 6
  • 14

1 Answers1

2
df %>% 
  select(-Score) %>%
  pivot_wider(names_from = Class, values_from = standard)

# A tibble: 4 x 4
  Subject A     B     C    
  <chr>   <chr> <chr> <chr>
1 Math    A     B     C    
2 ELA     U     B     U    
3 PE      NA    U     NA   
4 Art     NA    NA    C 

Then rename first column if necessary

denisafonin
  • 1,116
  • 1
  • 7
  • 16