[R] Data-frame selection

Cacique Samurai caciquesamurai at gmail.com
Sat Oct 10 17:38:00 CEST 2015


Hello R-Helpers!

I have a data-frame as below (dput in the end of mail) and need to
select just the first sequence of occurrence of each "Group" in each
"ID".

For example, for ID "1" I have two sequential occurrences of T2 and
two sequential occurrences of T3:

> test [test$ID == 1, ]
   ID Group  Var
3   1    T2 2.94
4   1    T2 3.23
5   1    T2 1.40
6   1    T2 1.62
7   1    T2 2.43
8   1    T2 2.53
9   1    T2 2.25
10  1    T3 1.66
11  1    T3 2.86
12  1    T3 0.53
13  1    T3 1.66
14  1    T3 3.24
15  1    T3 1.34
16  1    T2 1.86
17  1    T2 3.03
18  1    T3 3.63
19  1    T3 2.78
20  1    T3 1.49

As output, I need just the first group of T2 and T3 for this ID, like:

 ID Group  Var
3   1    T2 2.94
4   1    T2 3.23
5   1    T2 1.40
6   1    T2 1.62
7   1    T2 2.43
8   1    T2 2.53
9   1    T2 2.25
10  1    T3 1.66
11  1    T3 2.86
12  1    T3 0.53
13  1    T3 1.66
14  1    T3 3.24
15  1    T3 1.34

For others ID I have just one occurrence or sequence of occurrence of
each Group.

I tried to use a labeling variable, but cannot figure out do this
without many many loops..

Thanks in advanced,

Raoni

 dput (teste)
structure(list(ID = structure(c(3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2",
"3", "4"), class = "factor"), Group = structure(c(1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L), .Label = c("T2",
"T3"), class = "factor"), Var = c(0.32, 1.59, 2.94, 3.23, 1.4,
1.62, 2.43, 2.53, 2.25, 1.66, 2.86, 0.53, 1.66, 3.24, 1.34, 1.86,
3.03, 3.63, 2.78, 1.49, 2, 2.39, 1.65, 2.05, 2.75, 2.23, 1.39,
2.66, 1.05, 2.52, 2.49, 2.97, 0.43, 1.36, 0.79, 1.71, 1.95, 2.73,
2.73, 2.39, 2.17, 2.34, 2.42, 1.75, 0.66, 1.64, 0.24, 2.11, 2.11,
1.18)), .Names = c("ID", "Group", "Var"), row.names = c(NA, 50L
), class = "data.frame")



More information about the R-help mailing list