Message-ID: <49b639f1-cd58-5c85-24f7-f4066feebb38@sapo.pt>
Date: 2022-03-11T19:31:50Z
From: Rui Barradas
Subject: stdev error
In-Reply-To: <00b601d83570$69dd7500$3d985f00$@sbcglobal.net>
Hello,
Yes, you're right.
Thanks for posting this, in my original post unlike what I thought I was
able to reproduce the error. All stdev values were NA when in fact after
changing the mean to hp1 = mean(hp) some of them are not, there are
zeros in the output column stdev.
Rui Barradas
?s 17:49 de 11/03/2022, Jeff Reichman escreveu:
> Rui
>
> Found my problem, or at least I think I found the problem.
>
> # BEWARE: reusing variables may lead to unexpected results - https://dplyr.tidyverse.org/reference/summarise.html
>
> I changed my variable name and problem resolved.
>
> Jeff
>
> -----Original Message-----
> From: Rui Barradas <ruipbarradas at sapo.pt>
> Sent: Friday, March 11, 2022 9:24 AM
> To: reichmanj at sbcglobal.net; r-help at r-project.org
> Subject: Re: [R] stdev error
>
> Hello,
>
> I cannot reproduce this error with a built-in data set.
> Can you post str(my_tbl)?
>
>
> suppressPackageStartupMessages(library(dplyr))
>
> mtcars %>%
> mutate(hp = round(hp * 2) / 2) %>%
> group_by(cyl, hp) %>%
> summarise(
> count = n(),
> hp = mean(hp),
> stdev = sd(hp)
> )
> #> `summarise()` has grouped output by 'cyl'. You can override using the `.groups` #> argument.
> #> # A tibble: 23 x 4
> #> # Groups: cyl [3]
> #> cyl hp count stdev
> #> <dbl> <dbl> <int> <dbl>
> #> 1 4 52 1 NA
> #> 2 4 62 1 NA
> #> 3 4 65 1 NA
> #> 4 4 66 2 NA
> #> 5 4 91 1 NA
> #> 6 4 93 1 NA
> #> 7 4 95 1 NA
> #> 8 4 97 1 NA
> #> 9 4 109 1 NA
> #> 10 4 113 1 NA
> #> # ... with 13 more rows
>
> Hope this helps,
>
> Rui Barradas
>
>
> ?s 14:14 de 11/03/2022, Jeff Reichman escreveu:
>> 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 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.
>