Search code examples
rcastingreshape2melt

R how to reshape data as a column variable to become rownames and extract specific other variable data?


I have some trouble to convert my melted data from long to wide and also filter at the same time. I can proceed to subset before but I would like to do them at the same time and save them as different tables.

ID,freq,Subject,Time
aaaaa,5545,M1,0
aaaaa,5471,M2,0
aaaaa,5029,M3,0
aaaaa,4531,M1,3h
aaaaa,4523,M2,3h
aaaaa,3915,M3,3h
aaaaa,3800,M1,1day
aaaaa,3609,M2,1day
aaaaa,3427,M3,1day
bbbb,3426,M1,0
bbbb,3272,M2,0
bbbb,3266,M3,0
bbbb,5545,M1,3h
bbbb,5471,M2,3h
bbbb,5029,M3,3h
bbbb,4532,M2,1day
bbbb,4533,M3,1day

this is a snipnet from my data. I would like to obtain a table like below and save every subject in a different object(M1, M2, and M3...). I read and try to reshape and cast commands but could not manage what I need. how can you manage a table like below? Thank for the help and suggestions.

ID     0     3h   1day
aaaaa  5545 4531 3800
bbbb   3426 5545 4531

Solution

  • Using dcast you can do:

    df <- reshape2::dcast(df, ID + Subject ~ Time, value.var = "freq")
    df_list <- split(x, x$Subject)
    
    $M1
                    ID Subject    0 1day   3h
    1            aaaaa      M1 5545 3800 4531
    4             bbbb      M1 3426   NA 5545
    
    $M2
                    ID Subject    0 1day   3h
    2            aaaaa      M2 5471 3609 4523
    5             bbbb      M2 3272 4532 5471
    
    

    Another option is:

    library(dplyr)
    library(tidyr)
    
    df %>% 
    spread(Time, freq) %>% 
    group_split(., Subject)
    

    Data

    df <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("           aaaaa", 
    "           bbbb"), class = "factor"), freq = c(5545L, 5471L, 
    5029L, 4531L, 4523L, 3915L, 3800L, 3609L, 3427L, 3426L, 3272L, 
    3266L, 5545L, 5471L, 5029L, 4532L, 4533L), Subject = structure(c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 2L, 3L
    ), .Label = c("M1", "M2", "M3"), class = "factor"), Time = structure(c(1L, 
    1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L
    ), .Label = c("0", "1day", "3h"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -17L))