Bug 16564 - In data.frames var and sd of columns of text returns values, not NA
Summary: In data.frames var and sd of columns of text returns values, not NA
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Language (show other bugs)
Version: R 3.2.2
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-10-14 19:37 UTC by Dieter Kadelka
Modified: 2015-12-14 13:45 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dieter Kadelka 2015-10-14 19:37:57 UTC
> x = data.frame(a = c(1,2,3), b = c('u','v','w'))
> sapply(sd,x)
returns
1 1
not the correct 1 NA

Similarly
> sd(x$b) returns 1, not 0

> mean(x$b) returns the correct NA

Remark: Version 2.*.* of R seem to be o.K.
Comment 1 Duncan Murdoch 2015-10-14 20:00:36 UTC
This is about factors, not text columns of dataframes (but data.frame() converts text to factors by default, so your confusion is excused). 

b <- factor(letters)
sd(b)

Many functions (e.g. sum(), mean()) refuse to work on factors, but var() and sd() don't.  They probably should.
Comment 2 Martin Maechler 2015-10-30 17:12:23 UTC
I agree with Duncan ... and have made the changes:

------------------------------------------------------------------------
r69584 | maechler | 2015-10-30 18:11:49 +0100 (Fri, 30 Oct 2015) | 1 line
Changed paths:
   M doc/NEWS.Rd
   M src/library/stats/R/sd.R
   M src/library/stats/man/sd.Rd
   M src/library/stats/src/cov.c
   M tests/reg-tests-1c.R

var() and hence sd() now signal an error for factor arguments
------------------------------------------------------------------------