[R] add a variable a data frame to sequentially count unique rows

Yuan Chun Ding ycd|ng @end|ng |rom coh@org
Sat Jul 3 01:44:35 CEST 2021


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 using gmail.com] 
Sent: Friday, July 2, 2021 4:36 PM
To: Yuan Chun Ding <ycding using coh.org>
Cc: r-help using 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 using coh.org> wrote:
>
> Hi R users,
>
> In this test file,
> 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 have group 1 and group2 variable and want to add the count variable to sequentially count unique rows defined by group1 and group2.
>
> I hope to use the following functions in library (tidyverse),  No one worked well.
> test %>% group_by(group1, group2) %>% mutate(count = row_number()) 
> test %>% group_by(group1, group2) %>% mutate(count = 1:n()) test %>% 
> group_by(group1, group2) %>% mutate(count = seq_len(n())) test %>% 
> group_by(group1, group2) %>% mutate(count = seq_along(group1, group2))
>
> Can you help me to make the third column in the test data frame?
>
> Thank you,
>
> Ding
>
> ----------------------------------------------------------------------
> ------------------------------------------------------------
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the 
> individual or entity to which they are addressed. This communication 
> may contain information that is privileged, confidential, or exempt 
> from disclosure under applicable law (e.g., personal health 
> information, research data, financial information). Because this 
> e-mail has been sent without encryption, individuals other than the 
> intended recipient may be able to view the information, forward it to 
> others or tamper with the information without the knowledge or consent 
> of the sender. If you are not the intended recipient, or the employee 
> or person responsible for delivering the message to the intended 
> recipient, any dissemination, distribution or copying of the 
> communication is strictly prohibited. If you received the 
> communication in error, please notify the sender immediately by 
> replying to this message and deleting the message and any accompanying 
> files from your system. If, due to the security risks, you do not wish 
> to receive further communications via e-mail, please reply to this 
> message and inform the sender that you do not wish to receive further 
> e-mail from the sender. (LCP301)
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-he
> lp__;!!Fou38LsQmgU!49KQUSjG8hBMc_vqsnkIV0zOGGrGt0DjnjV167p8sI20Fzx-913
> RxHIHpfjS$ PLEASE do read the posting guide 
> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.htm
> l__;!!Fou38LsQmgU!49KQUSjG8hBMc_vqsnkIV0zOGGrGt0DjnjV167p8sI20Fzx-913R
> xDpiVYEa$ and provide commented, minimal, self-contained, reproducible 
> code.


More information about the R-help mailing list