Skip to content
Prev 388440 / 398502 Next

add a variable a data frame to sequentially count unique rows

Hi Bert,

I am very sorry that I made a mistake for the second count value. The original data are much more complicated, I just made a toy table.


 test  <- data.frame(group1=c("g1", "g1", "g1", "g2", "g2", "g2", "g2", "g2", "g2"),
                                        group2=c("k1", "a2", "a2", "c5", "n6", "n6", "n6", "m10","m10"),
                                         count=   c( 1,       2,        2,       1,      2,   2,     2,    3,    3 ));

I want to get count column.

the summary count is below.
count_test <- test %>% group_by(group1 ) %>%summarise(Number_of_region = n_distinct(group2))

Thank you,

Ding

-----Original Message-----
From: Bert Gunter [mailto:bgunter.4567 at gmail.com] 
Sent: Friday, July 2, 2021 4:36 PM
To: Yuan Chun Ding <ycding at coh.org>
Cc: r-help at r-project.org
Subject: Re: [R] add a variable a data frame to sequentially count unique rows

Your example makes no sense (to me, anyway). Please check it carefully. Note that the count in rows 2 and 3 increment but the counts in rows 5-7 or rows 8-9 do not. So your specification seems inconsistent to me.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Fri, Jul 2, 2021 at 3:27 PM Yuan Chun Ding <ycding at coh.org> wrote:
Message-ID: <SJ0PR02MB7645C19573F96B1CEE17D92ED41F9@SJ0PR02MB7645.namprd02.prod.outlook.com>
In-Reply-To: <CAGxFJbREoFz-QRFgCNT3zrwkCeUYQAzUGiCkoM-eY2T6MeQS7w@mail.gmail.com>