简体   繁体   中英

memory exhausted while trying to remove sd=0 column

I'm trying to remove numeric columns with sd=0, and this error "Error: memory exhausted (limit reached?)" pops up, my "data" was read with fread.

data2 <- data[ - as.numeric(which(apply(data, 2, var) == 0))]
Error: memory exhausted (limit reached?)

Is there a way around this?

Try using the data.table syntax:

library(data.table)
idx <- data[, sapply(.SD, function(col) var(col)==0)]
data2 <- data[, -idx, with=FALSE]

您可以使用colVarsmatrixStats一揽子方案通常快。

data2 <- data[matrixStats::colVars(as.matrix(data)) != 0, ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM