Any #rstats people know why the sd(x) function in base R doesn't work but sapply(x, sd) does? Is everyone just piping things into the sd function these days? Is there a secret sd function club I'm not a member of? Seems kind of an important, basic stats operation to not work.
2
2
3
Provide a MCVE or it doesn't count :) > sd(1:10) [1] 3.02765 > set.seed(123); sd(rnorm(10)) [1] 0.953784 > Is you 'x' a list or data.frame? Well yes in that case you need to loop over columns.
1
A dataframe. > sd(shark) Error in is.data.frame(x) : 'list' object cannot be coerced to type 'double' > sapply(shark,sd) Length 2.549928
3
Yup, rings a bell. Might be historical compatibility. There are add-on packages with column-wise variance or sd. Because this is a cludge -- but been-there-done-that: > library(palmerpenguins) > sapply(penguins, function(x) if (is.numeric(x)) sd(na.omit(x)) else NULL)
1
2
Doing mean() and sd() to whole data frames got removed in 3.0.0
3
7
Hah. That is so long ago that I even forgot it existed. Signed, old R user since 0.65 or so.

Sep 18, 2021 · 11:30 PM UTC

1