Search code examples
rstrsplit

how to transform rows to columns in R


I would like to know how to transform rows to columns for the following dataset.

School class Avg  Subavg  Sub
ABC     2    25.3  17.2   Geo
ABC     2    25.3  18.2   Mat
ABC     2    25.3  20.2   Fre
ABC     3    21.2  17.2   Geo
ABC     3    21.2  18.2   Mat
ABC     3    21.2  20.2   Ger
ABC     4    16.8  17.2   Ger
ABC     4    16.8  18.2   Mat
ABC     5    20.2  20.2   Fre

Expected output would be

School Std stdavg  Geo   mat    Ger     Fer
ABC    2    25.3   17.2  18.2   NA      20.2
ABC    3    21.2   17.2  18.2   20.2    NA
ABC    4    25.3   NA    18.2   17.2    NA
ABC    5    25.3   NA    NA     NA      20.2

I used split function, But in vain.

Thanks in advance


Solution

  • We can use dcast

    library(data.table)
    dcast(setDT(df1), School+class+Avg~Sub, value.var="Subavg")
    #    School class  Avg  Fre  Geo  Ger  Mat
    #1:    ABC     2 25.3 20.2 17.2   NA 18.2
    #2:    ABC     3 21.2   NA 17.2 20.2 18.2
    #3:    ABC     4 24.8   NA   NA 17.2 18.2
    #4:    ABC     5 24.8 20.2   NA   NA   NA
    

    Or use spread from tidyr

    library(tidyr)
    spread(df1, Sub, Subavg)