Используя dcast
, вы можете сделать:
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
Другой вариант:
library(dplyr)
library(tidyr)
df %>%
spread(Time, freq) %>%
group_split(., Subject)
Данные
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))