[R] define subgroups based on position in table
jim holtman
jholtman at gmail.com
Tue Jul 20 15:15:05 CEST 2010
try this:
> x <- read.table(textConnection("group
+ A
+ A
+ A
+ B
+ B
+ A
+ A
+ B
+ B
+ B
+ A
+ A"),header=TRUE, as.is=TRUE)
> closeAllConnections()
> # use rle to get the 'runs'
> x.rle <- rle(x$group)
> # now generate the group numbers
> x.grp <- ave(x.rle$values, x.rle$values, FUN=seq_along)
> # put the numbers back
> x$sub <- with(x.rle, paste(rep(values, lengths), rep(x.grp, lengths), sep=''))
>
>
> x
group sub
1 A A1
2 A A1
3 A A1
4 B B1
5 B B1
6 A A2
7 A A2
8 B B2
9 B B2
10 B B2
11 A A3
12 A A3
>
On Tue, Jul 20, 2010 at 8:02 AM, STEVENS, Maarten
<Maarten.STEVENS at inbo.be> wrote:
> Dear list,
>
> I have a data frame with one column (group) and want to add a second column (sub) with a serial number that says to which subgroup a cell belongs. A subgroup contains the consecutive rows of the same group. The number of a subgroup is based on its position in the table. The first subgroup of A's and B's should have nr A1 and B1, the second nr A2 and B2, etc.. I hope the example below makes things more clear.
> This is what my data look like:
>
> x <- read.table(textConnection("group
> A
> A
> A
> B
> B
> A
> A
> B
> B
> B
> A
> A"),header=TRUE)
> closeAllConnections()
>
> group
> 1 A
> 2 A
> 3 A
> 4 B
> 5 B
> 6 A
> 7 A
> 8 B
> 9 B
> 10 B
> 11 A
> 12 A
>
> This is what it should become:
>
> group sub
> 1 A A1
> 2 A A1
> 3 A A1
> 4 B B1
> 5 B B1
> 6 A A2
> 7 A A2
> 8 B B2
> 9 B B2
> 10 B B2
> 11 A A3
> 12 A A3
>
>
>
>
>
> Maarten Stevens
>
> Senior onderzoeker - Aquatisch Beheer / Senior scientist - Aquatic Management
>
>
>
> INBO – Instituut voor Natuur- en Bosonderzoek / Research Institute for Nature and Forest
>
> Afdeling Beheer en Duurzaam Gebruik / Dept. Management and Sustainable Use
>
> Kliniekstraat 25 Ÿ B-1070 Brussels, BELGIUM
>
> E-mail: maarten.stevens at inbo.be <mailto:maarten.stevens at inbo.be>
>
> Phone: +32.2.558.19.18 Ÿ Gsm / Cell: +32.492.72.28.97 Ÿ Fax: +32.2.558.18.05 Ÿ www.inbo.be <http://www.inbo.be/>
>
>
>
>
>
>
>
> Druk dit bericht a.u.b. niet onnodig af.
> Please do not print this message unnecessarily.
>
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
> en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
> door een geldig ondertekend document. The views expressed in this message
> and any annex are purely those of the writer and may not be regarded as stating
> an official position of INBO, as long as the message is not confirmed by a duly
> signed document.
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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.
>
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list