user297850
user297850

Reputation: 8015

remove or find NaN in R

I have a data.frame x2 as

> x2
    x2
 1 NaN
 2 0.1
 3 NaN
 4 0.2
 5 0.3

I would like to remove the NaN from this column. Is there a quick way to do that?

Upvotes: 14

Views: 91063

Answers (4)

Andrey Shabalin
Andrey Shabalin

Reputation: 4614

Simply this

x2 = x2[!is.na(x2)];

Upvotes: 4

davsjob
davsjob

Reputation: 1960

An easy solution could be:

x2 <- c(NaN, 0.1, NaN, 0.2, 0.3)

library(hablar)

s(x2)

which gives you:

[1] 0.1 0.2 0.3

Upvotes: 2

Vyga
Vyga

Reputation: 903

The function subset could be useful for using complex indicators.

subset(x2, !is.nan(x2[[1]]))

Upvotes: 2

lennon310
lennon310

Reputation: 12709

x2 <- na.omit(x2)

may work for you.

Upvotes: 34

Related Questions