Try

subset(Dat, AA == "A" | (AA == "B" & BB == "b"))

HTH,
Jorge.-


On Wed, Apr 24, 2013 at 8:21 PM, Christofer Bogaso <
bogaso.christofer@gmail.com> wrote:

> Hello again,
>
> Let say I have following data:
>
> Dat <- structure(list(AA = structure(c(3L, 1L, 2L, 1L, 2L, 3L, 3L, 2L,
> 3L, 1L, 1L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 1L, 1L), .Label = c("A",
> "B", "C"), class = "factor"), BB = structure(c(2L, 3L, 2L, 2L,
> 2L, 3L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 2L, 3L
> ), .Label = c("a", "b", "c"), class = "factor"), CC = 1:20), .Names =
> c("AA",
> "BB", "CC"), row.names = c(NA, -20L), class = "data.frame")
>
> Now I want to select a subset of that 'Dat', for which:
> 1. First column will contain ALL "A"
> 2. First column will contain those "B" for which "BB = b" in second column.
>
> Therefore I tries following:
>
> > Only_A <- Dat[Dat[, 'AA'] == "A", ]
> > Only_B <- Dat[Dat[, 'AA'] == "B", ]
> > rbind(Only_A, Only_B[Only_B[, 'BB'] == "b", ])
>    AA BB CC
> 2   A  c  2
> 4   A  b  4
> 10  A  a 10
> 11  A  a 11
> 18  A  b 18
> 19  A  b 19
> 20  A  c 20
> 3   B  b  3
> 5   B  b  5
> 8   B  b  8
>
>
> However I believe there must be some better code to achieve that which
> is tidier, i.e. there must be some 1-liner code.
>
> Can somebody suggest any better approach if possible?
>
> Thanks and regards,
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

