数据帧的标准偏差不起作用

Hen*_*enk 12 r

在计算标准偏差时,我有一个意想不到的[至少对我来说]错误.想法[*]是将所有缺失值转换为1和0否则.然后在完成关联之前提取具有一些[但不是全部]缺失值的变量.使用sd函数尝试提取步骤,但它失败[为什么?].

library(VIM)
data(sleep) # dataset with missing values

x = as.data.frame(abs(is.na(sleep))) # converts all NA to 1, otherwise 0
y = x[which(sd(x) > 0)] # attempt to extract variables with missing values

Error in is.data.frame(x) : 
(list) object cannot be coerced to type 'double'

# convert to double    
z = as.data.frame(apply(x, 2, as.numeric))
y = z[which(sd(z) > 0)]

Error in is.data.frame(x) : 
(list) object cannot be coerced to type 'double'
Run Code Online (Sandbox Code Playgroud)

[*] R in Action,Robert Kabacoff

Jos*_*ich 16

sd 自R-3.0.0起,data.frames已经不存在了:

> ## Build a db of all R news entries.
> db <- news()
> ## sd
> news(grepl("sd", Text), db=db)
Changes in version 3.0.3:

PACKAGE INSTALLATION

    o   The new field SysDataCompression in the DESCRIPTION file allows
        user control over the compression used for sysdata.rda objects in
        the lazy-load database.

Changes in version 3.0.0:

DEPRECATED AND DEFUNCT

    o   mean() for data frames and sd() for data frames and matrices are
        defunct.
Run Code Online (Sandbox Code Playgroud)

sapply(x, sd)改用.

  • @Henk如果您不想浏览遗留代码并进行更改,可以轻松定义自己的`mean.data.frame`和`sd.data.frame`函数. (5认同)