# [R] Split a data.frame

arun smartpink111 at yahoo.com
Sun Oct 27 17:48:52 CET 2013

```Hi,
DF\$Col2
# [1] a e b b a b c e b a c c e e a c d c c e
#Levels: a b c d e

"b" is not found in any of the "Grs".  Also Gr3 is not presnt in DF\$Col2

So, I am not sure whether this works for you.

indx <- 1+ 2*DF\$Col2 %in% Gr1 + 4*DF\$Col2 %in% Gr2 + 8*DF\$Col2 %in% Gr3
indx <- indx[indx>1]
split(DF[DF\$Col2%in% c(Gr1,Gr2,Gr3),],indx)

A.K.

On Sunday, October 27, 2013 11:19 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:
Hi again,

Let say I have following DF:

DF <- structure(list(Col1 = 1:20, Col2 = structure(c(1L, 5L, 2L, 2L,
1L, 2L, 3L, 5L, 2L, 1L, 3L, 3L, 5L, 5L, 1L, 3L, 4L, 3L, 3L, 5L
), .Label = c("a", "b", "c", "d", "e"), class = "factor")), .Names =
c("Col1",
"Col2"), row.names = c(NA, -20L), class = "data.frame")

DF

Now I create 3 groups like:

Gr1 <- c('a', 'c', 'd')
Gr2 <- c('e')
Gr3 <- c('f', 'x')

My goal is to split DF according to these groups. And to generate NULL (or
something like that) if a particular group contains no value.

So far I tried to split DF according to split() function. However it looks
to me like, this split() does not offer this kind of customization.

Can someone here help me how to split my data.frame according to this
criteria?

Thanks and regards,

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help