I wonder how to split my toy data
below such that I get a list of smaller dataf.rames each of which containing a unique pair of group
in it?
My desired_output
is shown below. Is such a subsetting possible in R
?
As this is a toy data
, I highly appreciate a functional answer where the names of the columns or their numbers can be different than this toy data
.
m=
"
y group time outcome
7 a 1 A
3 a 0 B
6 a 1 B
5 b 0 A
9 b 1 A
4 b 0 B"
data <- read.table(text = m, h=T)
desired_output <-
list(
data.frame(y = 5 , group = "b" , time = 0 , outcome = "A" ),
data.frame(y = c(7,9), group = c("a","b"), time = c(1,1), outcome = c("A","A")),
data.frame(y = c(3,4), group = c("a","b"), time = c(0,0), outcome = c("A","A")),
data.frame(y = 6 , group = "a" , time = 1 , outcome = "B"))
[[1]]
y group time outcome
1 5 b 0 A
[[2]]
y group time outcome
1 7 a 1 A
2 9 b 1 A
[[3]]
y group time outcome
1 3 a 0 A
2 4 b 0 A
[[4]]
y group time outcome
1 6 a 1 B
CodePudding user response:
Assuming that the question is asking to split the input into rows that have the same time and outcome we can use split
as follows. No packages are used.
split(data, data[3:4])
giving:
$`0.A`
y group time outcome
4 5 b 0 A
$`1.A`
y group time outcome
1 7 a 1 A
5 9 b 1 A
$`0.B`
y group time outcome
2 3 a 0 B
6 4 b 0 B
$`1.B`
y group time outcome
3 6 a 1 B