[R] Grouping Question

Chris Evans chr|@ho|d @end|ng |rom p@yctc@org
Sun Mar 22 07:30:30 CET 2020


Here's a very "step by step" example with dplyr as I'm trying to teach myself the Tidyverse way of being

library(dplyr)

# Serial        Measurement        Meas_test        Serial_test
# 1        17                fail                fail
# 1        16                pass                fail
# 2        12                pass                pass
# 2        8                pass                pass
# 2        10                pass                pass
# 3        19                fail                fail
# 3        13                pass                pass

dat <- as.data.frame(list(Serial = c(1,1,2,2,2,3,3),
                          Measurement = c(17, 16, 12, 8, 10, 19, 13),
                          Meas_test = c("fail", "pass", "pass", "pass", "pass", "fail", "pass")))

dat %>%
  group_by(Serial) %>%
  summarise(Serial_test = sum(Meas_test == "fail")) %>%
  mutate(Serial_test = if_else(Serial_test > 0, 1, 0),
         Serial_test = factor(Serial_test,
                              levels = 0:1,
                              labels = c("pass", "fail"))) -> groupedDat

dat %>%
  left_join(groupedDat) # add -> dat to the end to pip to dat

Gives:

  Serial Measurement Meas_test Serial_test
1      1          17      fail        fail
2      1          16      pass        fail
3      2          12      pass        pass
4      2           8      pass        pass
5      2          10      pass        pass
6      3          19      fail        fail
7      3          13      pass        fail
            
Would be easier for us if used dput() to share your data but thanks for the minimal example!

Chris

----- Original Message -----
> From: "Ivan Krylov" <krylov.r00t using gmail.com>
> To: "Thomas Subia via R-help" <r-help using r-project.org>
> Cc: "Thomas Subia" <tgs77m using yahoo.com>
> Sent: Sunday, 22 March, 2020 07:24:15
> Subject: Re: [R] Grouping Question

> On Sat, 21 Mar 2020 20:01:30 -0700
> Thomas Subia via R-help <r-help using r-project.org> wrote:
> 
>> Serial_test is a pass, when all of the Meas_test are pass for a given
>> serial. Else Serial_test is a fail.
> 
> Use by/tapply in base R or dplyr::group_by if you prefer tidyverse
> packages.
> 
> --
> Best regards,
> Ivan
> 
> ______________________________________________
> R-help using 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.

-- 
Chris Evans <chris using psyctc.org> Visiting Professor, University of Sheffield <chris.evans using sheffield.ac.uk>
I do some consultation work for the University of Roehampton <chris.evans using roehampton.ac.uk> and other places
but <chris using psyctc.org> remains my main Email address.  I have a work web site at:
   https://www.psyctc.org/psyctc/
and a site I manage for CORE and CORE system trust at:
   http://www.coresystemtrust.org.uk/
I have "semigrated" to France, see: 
   https://www.psyctc.org/pelerinage2016/semigrating-to-france/ 
That page will also take you to my blog which started with earlier joys in France and Spain!

If you want to book to talk, I am trying to keep that to Thursdays and my diary is at:
   https://www.psyctc.org/pelerinage2016/ceworkdiary/
Beware: French time, generally an hour ahead of UK.



More information about the R-help mailing list