[R] Looking for a better code for my problem.
Christofer Bogaso
bogaso.christofer at gmail.com
Wed Apr 24 12:21:43 CEST 2013
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,
More information about the R-help
mailing list