remove row with nan value

和自甴很熟 提交于 2019-11-27 12:06:30

问题


let's say, for example, i have this data:

data <- c(1,2,3,4,5,6,NaN,5,9,NaN,23,9)
attr(data,"dim") <- c(6,2)
data

     [,1] [,2]
[1,]    1  NaN
[2,]    2    5
[3,]    3    9
[4,]    4  NaN
[5,]    5   23
[6,]    6    9

Now i want to remove the rows with the NaN values in it: row 1 and 4. But i don't know where these rows are, if it's a dataset of 100.000+ rows, so i need to find them with a function and remove the complete row.

Can anybody point me in the right direction?


回答1:


The function complete.cases will tell you where the rows are that you need:

data <- matrix(c(1,2,3,4,5,6,NaN,5,9,NaN,23,9), ncol=2)
data[complete.cases(data), ]

     [,1] [,2]
[1,]    2    5
[2,]    3    9
[3,]    5   23
[4,]    6    9



回答2:


DATA

data <- matrix(rnorm(100000000), ncol=10000)
idx <- sample(1:10000,100)
idy <- sample(1:10000,100)
data[idx,idy] <- NaN

FUNCTION

foo <- function(d=data) d[apply(d,2,function(x) any(is.nan(x))),]

RUNTIMES

> system.time(data[complete.cases(data),])
   user  system elapsed 
   3.53    0.12    3.65 

> system.time(foo(data))
   user  system elapsed 
   2.37    0.09    2.46 


来源:https://stackoverflow.com/questions/5961839/remove-row-with-nan-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!