Yuan Chun Ding
2021-Jul-02 22:27 UTC
[R] add a variable a data frame to sequentially count unique rows
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)
Jeff Newmiller
2021-Jul-02 22:35 UTC
[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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
Bert Gunter
2021-Jul-02 23:35 UTC
[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:> > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Avi Gross
2021-Jul-03 00:13 UTC
[R] add a variable a data frame to sequentially count unique rows
Ding, Just to get you to stop asking, here is a solution I hope works. In English, if you are asking that ONE instance of a duplicate be marked in a new column with TRUE or 1 while all remaining ones are marked as FALSE or 2 or whatever, that is easy enough. The method is to use the assistive function row_number() inside a grouped mutate() and only one item has a row number of 1 and all others higher. If I load your test variable (see below) I can add another column I called count2 fairly easily with this: test %>% group_by(group1,group2) %>% mutate(count2 = ifelse(row_number()==1, TRUE, FALSE)) %>% ungroup() The output matches your first count variable made by hand:> test %>%+ group_by(group1,group2) %>% + mutate(count2 = ifelse(row_number()==1, TRUE, FALSE)) %>% + ungroup() # A tibble: 9 x 4 group1 group2 count count2 <chr> <chr> <dbl> <lgl> 1 g1 k1 1 TRUE 2 g1 a2 1 TRUE 3 g1 a2 2 FALSE 4 g2 c5 1 TRUE 5 g2 n6 2 TRUE 6 g2 n6 2 FALSE 7 g2 n6 2 FALSE 8 g2 m10 3 TRUE 9 g2 m10 3 FALSE Now if you actually want to have a count of first and 2nd and third, it is even easier: test %>% group_by(group1,group2) %>% mutate(counter = row_number()) %>% ungroup() Unfortunately for you, my version of output suggest you made a mistake on the last row with g2/n6:> test %>%+ group_by(group1,group2) %>% + mutate(counter = row_number()) %>% + ungroup() # A tibble: 9 x 4 group1 group2 count counter <chr> <chr> <dbl> <int> 1 g1 k1 1 1 2 g1 a2 1 1 3 g1 a2 2 2 4 g2 c5 1 1 5 g2 n6 2 1 6 g2 n6 2 2 7 g2 n6 2 3 8 g2 m10 3 1 9 g2 m10 3 2 There are of course other ways to do such things but the above seems simple enough. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Yuan Chun Ding Sent: Friday, July 2, 2021 6:27 PM To: r-help at r-project.org Subject: [R] add a variable a data frame to sequentially count unique rows 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Rui Barradas
2021-Jul-03 07:28 UTC
[R] add a variable a data frame to sequentially count unique rows
Hello, Either I'm not understanding or isn't this just any of aggregate(count ~ ., data = test, FUN = length) test %>% count(group1, group2, name = "Count") ? Hope this helps, Rui Barradas ?s 23:27 de 02/07/21, Yuan Chun Ding escreveu:> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >