[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 02:01:58 CEST 2021


Hi Jeff,
Your code generated the count what I wanted.    

As I emailed to Bert, yes, I made a mistake when generating the toy table,  the second count value should be 2.
Correct file is 
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 also thought about removing of duplicates first and then doing a merge.  For some reason,  I  thought  one line of code  from tidyverse library could do it .

Thank You,

Ding

-----Original Message-----
From: Jeff Newmiller [mailto:jdnewmil using dcn.davis.ca.us] 
Sent: Friday, July 2, 2021 4:51 PM
To: Yuan Chun Ding <ycding using coh.org>
Cc: r-help using r-project.org; r-help using r-project.org
Subject: RE: [R] add a variable a data frame to sequentially count unique rows

I am not sure I understand... you are referring to files but we are not discussing files here. There are "input" and "output" phases yet to be dealt with that separate the analysis you do in R from the files themselves. We are discussing data frames here.

Regardless, you either have duplicates in your input (faulty data) or you do not have duplicates because there are additional distinguishing columns that you have not identified. (https://urldefense.com/v3/__https://en.wikipedia.org/wiki/Primary_key__;!!Fou38LsQmgU!9delUPpdk-OQmYXQ0xQaFd2NDqykIVLC403VICI0m2ihoIqtK3XP0PTJ7-ev$ )

The solution to the problem that you should not have involves making a sensible (not faulty) copy of your data, setting up the count column, and then joining the faulty data with the sensible data.

Note that the ordering of the result happens to be retained when using dplyr::left_join, but is not retained using most other join implementations (such as base::merge). There can be significant performance gains to be had by giving up on record ordering, which is one of the reasons why having a valid primary key can be so important.

library(dplyr)
lookup_df <- (   test
              %>% select( group1, group2 )
              %>% filter( !duplicated( . ) )
              %>% group_by( group1 )
              %>% mutate( count1 = seq.int( n() ) )
              %>% ungroup()
              )
ans <- left_join( test, lookup_df, by = c( "group1", "group2" ) )

  On Fri, 2 Jul 2021, Yuan Chun Ding wrote:

> Hi Jeff,
>
> Thank you for your quick response!!
> I made this following summary file, however, I want to add the count number sequentially into the original file, because I have several more columns to explain or annotate the first two columns.
>
> count_test <- test %>% group_by(group1 ) %>%summarise(Number_of_region 
> = n_distinct(group2))
>
>
> Ding
> -----Original Message-----
> From: Jeff Newmiller [mailto:jdnewmil using dcn.davis.ca.us]
> Sent: Friday, July 2, 2021 3:36 PM
> To: r-help using r-project.org; Yuan Chun Ding <ycding using coh.org>; 
> r-help using r-project.org
> Subject: Re: [R] add a variable a data frame to sequentially count 
> unique rows
>
> It is poor analytical design to keep duplicates. If they are not duplicates then there should be a distinguishing additional column. IMO you should re-think your analysis rather than accomplish this short-term goal only to find down the road that this duplication causes future problems.
>
> On July 2, 2021 3:27:21 PM PDT, 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,     1,    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-h
>> el 
>> p__;!!Fou38LsQmgU!6hr10CQQybFHF0rsNtkWtEHa_9iV_iYS8khF0fy8MDanYoQM40_
>> aN
>> bjXXLLn$
>> PLEASE do read the posting guide
>> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.ht
>> ml 
>> __;!!Fou38LsQmgU!6hr10CQQybFHF0rsNtkWtEHa_9iV_iYS8khF0fy8MDanYoQM40_a
>> NV Lja7f2$ and provide commented, minimal, self-contained, 
>> reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil using dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list