[R] stdev error

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Fri Mar 11 15:50:24 CET 2022


Hallo

with(my_tbl, aggregate(Bse_bwt, list(Cat), function(x) c(n=length(x), mean=mean(x), st_dev=sd(x))))

Or am I missing something?

Cheers
Petr


-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Chris Evans
Sent: Friday, March 11, 2022 3:22 PM
To: reichmanj using sbcglobal.net
Cc: r-help using r-project.org
Subject: Re: [R] stdev error

Can't see your data but perhaps:

my_tbl %>%
  mutate(Bse_bwt = round(Bse_bwt * 2) / 2) %>%
  group_by(Cat, Bse_bwt) %>%
  summarize(count = n(),
     Bse_ftv = mean(Bse_ftv),
     stdev = if_else(count > 1,
                     sd(Bse_ftv),
                     NA_real_))


----- Original Message -----
> From: "Jeff Reichman" <reichmanj using sbcglobal.net>
> To: r-help using r-project.org
> Sent: Friday, 11 March, 2022 15:14:52
> Subject: [R] stdev error

> r-help forum
>
>
>
> When I run the following code
>
>
>
> my_tbl %>%
>
>  mutate(Bse_bwt = round(Bse_bwt * 2) / 2) %>%
>
>  group_by(Cat, Bse_bwt) %>%
>
>  summarize(count = n(), Bse_ftv = mean(Bse_ftv), stdev = sd(Bse_ftv))
>
>
>
> I get the following error:
>
>
>
> Error: `stdev` refers to a variable created earlier in this summarise().
>
> Do you need an extra mutate() step?
>
>
>
> I suspect it is because the standard deviation of a length-one vector
> is NA and R is errorerrors out on the standard deviation  of 1. So
> then I tried
>
>
>
> summarize(count = n(), Bse_ftv = mean(Bse_ftv), stdev = if(n()>1)
> sd(Bse_ftv) else 0) and this didn't seem to work either. So there has
> to be a way to add some sort of error checker to my standard deviation
> function to check if n > 1 and then take the standard deviation in dplyr.
>
>
>
> Jeff
>
>
>
>
>[[alternative HTML version deleted]]
>
> ______________________________________________
> 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 (he/him) <chris using psyctc.org> Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, University of Roehampton, London, UK.
Work web site: https://www.psyctc.org/psyctc/
CORE site:     https://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
OMbook:        https://ombook.psyctc.org/book/

______________________________________________
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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/



More information about the R-help mailing list