I have a data frame with following structure:
pat <- c(rep(1,50), rep(2,50), rep(3,50))
inc <- rep(c(rep(1,5), rep(2,5), rep(3,5), rep(4,5), rep(5,5),
rep(6,5), rep(7,5), rep(8,5), rep(9,5), rep(10,5)), 3)
df <- data.frame(cbind(pat, inc))
df is split into a list of elements:
all.inc = split(df, inc)
Now I want to split each element of this list into sub-lists. Something like:
all.pat = split(all.inc, pat)
This doesn't work, obviously. I've already tried the plyr
functions and lapply
, but didn't get it to work.
Any ideas?
Use lapply
:
lapply(all.inc, function(x) split(x, x$pat))
If you'd like to split your data frame all at once, you could use
split(df, interaction(df$pat,df$inc))
However, the returned value will be a single list of data frames, which is slightly different from what you would get by splitting list elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With