【问题标题】:Replace all outliers across all columns in dataframe with NA用 NA 替换数据框中所有列的所有异常值
【发布时间】:2018-02-21 22:24:55
【问题描述】:

我有一个包含数字和因子变量组合的数据框。

我正在尝试用 NA 递归替换所有异常值(3 x SD),但是我遇到了以下错误的问题

Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

我使用的代码是

name = factor(c("A","B","NA","D","E","NA","G","H","H"))
height = c(120,NA,150,170,NA,146,132,210,NA)
age = c(10,20,0,30,40,50,60,NA,130)
mark = c(100,0.5,100,50,90,100,NA,50,210)
data = data.frame(name=name,mark=mark,age=age,height=height)
data
data[is.na(data)] <- 77777 
data.scale <-  scale(data)
data.scale[ abs(data.scale) > 3 ] <- NA
data <- data.scale

关于如何使它工作的任何建议?

【问题讨论】:

  • 包含reproducible example 会让其他人更容易帮助您。
  • 如果你在谈论异常值,你的变量可能不应该是一个因素
  • 您正在对不只包含数值的数据框进行数学应用。使用data = data.frame(mark=mark,age=age,height=height),不使用name 列。运行其余代码并在末尾添加行data&lt;-cbind(name,data)

标签: r dataframe na outliers


【解决方案1】:

这是一种方法:

library(dplyr)

# take note of order for column names
data.names <- colnames(data)

# scale all numeric columns
data.numeric <- select_if(data, is.numeric) %>% # subset of numeric columns
  mutate_all(scale)                             # perform scale separately for each column
data.numeric[data.numeric > 3] <- NA            # set values larger than 3 to NA (none in this example)

# combine results with subset data frame of non-numeric columns
data <- data.frame(select_if(data, function(x) !is.numeric(x)),
                   data.numeric)

# restore columns to original order
data <- data[, data.names]

> data
  name        mark         age     height
1    A  0.20461856 -0.80009469 -1.0844636
2    B -1.43232992 -0.55391171         NA
3   NA  0.20461856 -1.04627767 -0.1459855
4    D -0.61796862 -0.30772873  0.4796666
5    E  0.04010112 -0.06154575         NA
6   NA  0.20461856  0.18463724 -0.2711159
7    G          NA  0.43082022 -0.7090723
8    H -0.61796862          NA  1.7309707
9    H  2.01431035  2.15410109         NA

注意:在这种方法中,非数字(字符/因子/等)变量将排在数字变量之前。因此,最后一步恢复原始顺序(如果适用)。

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 2018-03-18
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2021-11-26
    相关资源
    最近更新 更多