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
Basically, yes. And warned of deprecation in 2.15.3.
(@Docker used with @Debian oldoldstable along with the incomparable snapshot.debian.org made this easy-ish. No compilation needed.)
#archeology #rstats
Sep 19, 2021 · 1:26 PM UTC
1
5




