29

收到此错误时,我正在使用 prcomp 函数

Error in prcomp.default(x, ...) : 
cannot rescale a constant/zero column to unit variance

我知道我可以手动扫描我的数据,但是 R 中是否有任何函数或命令可以帮助我删除这些常量变量?我知道这是一项非常简单的任务,但我从未遇到过任何执行此操作的函数。

谢谢,

4

5 回答 5

46

这里的问题是您的列方差等于零。您可以通过这种方式检查数据框的哪一列是恒定的,例如:

df <- data.frame(x=1:5, y=rep(1,5))
df
#   x y
# 1 1 1
# 2 2 1
# 3 3 1
# 4 4 1
# 5 5 1

# Supply names of columns that have 0 variance
names(df[, sapply(df, function(v) var(v, na.rm=TRUE)==0)])
# [1] "y" 

因此,如果要排除这些列,可以使用:

df[,sapply(df, function(v) var(v, na.rm=TRUE)!=0)]

编辑:事实上,它更容易使用apply。像这样的东西:

df[,apply(df, 2, var, na.rm=TRUE) != 0]
于 2013-02-25T14:17:23.133 回答
15

我猜这个问答是一个流行的谷歌搜索结果,但是对于一个大矩阵来说答案有点慢,而且我没有足够的声誉来评论第一个答案。因此,我发布了该问题的新答案。

对于大矩阵的每一列,检查最大值是否等于最小值就足够了。

df[,!apply(df, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE))]

这是测试。与第一个答案相比,减少了 90% 以上的时间。它也比对该问题的第二条评论的答案更快。

ncol = 1000000
nrow = 10
df <- matrix(sample(1:(ncol*nrow),ncol*nrow,replace = FALSE), ncol = ncol)
df[,sample(1:ncol,70,replace = FALSE)] <- rep(1,times = nrow) # df is a large matrix

time1 <- system.time(df1 <- df[,apply(df, 2, var, na.rm=TRUE) != 0]) # the first method
time2 <- system.time(df2 <- df[,!apply(df, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE))]) # my method
time3 <- system.time(df3 <- df[,apply(df, 2, function(col) { length(unique(col)) > 1 })]) # Keith's method

time1
#   user  system elapsed 
# 22.267   0.194  22.626 
time2
#   user  system elapsed 
#  2.073   0.077   2.155 
time3
#   user  system elapsed 
#  6.702   0.060   6.790
all.equal(df1, df2)
# [1] TRUE
all.equal(df3, df2)
# [1] TRUE
于 2016-03-02T11:55:30.680 回答
9

由于这个问答是一个流行的谷歌搜索结果,但是对于一个大矩阵来说答案有点慢,而且@raymkchow 版本对于 NA 来说很慢,我建议使用指数搜索和data.table功率的新版本。

这是我在dataPreparation包中实现的一个功能。

首先构建一个示例 data.table,行多于列(通常是这种情况)和 10% 的 NA

ncol = 1000
nrow = 100000
df <- matrix(sample(1:(ncol*nrow),ncol*nrow,replace = FALSE), ncol = ncol)
df <- apply (df, 2, function(x) {x[sample( c(1:nrow), floor(nrow/10))] <- NA; x} ) # Add 10% of NAs
df[,sample(1:ncol,70,replace = FALSE)] <- rep(1,times = nrow) # df is a large matrix
df <- as.data.table(df)

然后对所有方法进行基准测试:

time1 <- system.time(df1 <- df[,apply(df, 2, var, na.rm=TRUE) != 0, with = F]) # the first method
time2 <- system.time(df2 <- df[,!apply(df, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE)), with = F]) # raymkchow
time3 <- system.time(df3 <- df[,apply(df, 2, function(col) { length(unique(col)) > 1 }), with = F]) # Keith's method
time4 <- system.time(df4 <- df[,-which_are_constant(df, verbose=FALSE)]) # My method

结果如下:

time1 # Variance approch
#   user  system elapsed 
#   2.55    1.45    4.07
time2 # Min = max approach
#   user  system elapsed 
#  2.72      1.5    4.22
time3 # length(unique()) approach
#   user  system elapsed 
#    6.7    2.75    9.53
time4 # Exponential search approach
#   user  system elapsed 
#   0.39    0.07    0.45
all.equal(df1, df2)
# [1] TRUE
all.equal(df3, df2)
# [1] TRUE
all.equal(df4, df2)
# [1] TRUE

dataPreparation:which_are_constant比其他方法快 10 倍。

此外,您拥有的行越多,使用起来就越有趣。

于 2017-11-15T13:08:12.683 回答
0

如果您正在寻求在 df 中返回非常量变量的 dplyr 解决方案,我建议您使用以下方法。%>% colnames()或者,如果需要列名,您可以添加:

library(dplyr)
df <- data.frame(x = 1:5, y = rep(1,5))
# returns dataframe
var_df <- df %>%
  select_if(function(v) var(v, na.rm=TRUE) != 0)  
var_df %>% colnames() # returns column names
于 2020-09-24T16:36:56.627 回答
0

Keith 评论的 tidyverse 版本:

df %>% purrr::keep(~length(unique(.x)) != 1)
于 2022-02-24T10:18:32.247 回答