[R] stdev error

Jeff Reichman re|chm@nj @end|ng |rom @bcg|ob@|@net
Fri Mar 11 15:27:40 CET 2022


Well I can see my "ifelse" syntax is wrong so I've changed it to 

  summarize(count = n(), Bse_ftv = mean(Bse_ftv), stdev = ifelse(count>1,
sd(Bse_ftv),0)) but still getting

Error: `stdev` refers to a variable created earlier in this summarise().
Do you need an extra mutate() step?

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Reichman
Sent: Friday, March 11, 2022 8:15 AM
To: r-help using r-project.org
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 <mailto: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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list