-4

I have a variable in R. I am going to do PCA analysis. but I have a lot of NaN values. do you guys know how to get rid of them?

my data looks like this:

        11819 11820 11821      
s1  1.1547005 NaN  1.1547005 
s2 -0.5773503 NaN -0.5773503 
s4 -0.5773503 NaN -0.5773503 

11819, 11820 and 11821 are col names and s1, s2 and s4 are row names. thanks

user3925736
  • 99
  • 1
  • 1
  • 13
  • 1
    Is the whole column NaN? – Haboryme Sep 12 '16 at 09:05
  • yes the whole column is NaN – user3925736 Sep 12 '16 at 09:12
  • Appearance of NaNs in results is often indicative of a mistake on practitioner’s part. So before removing then, you better know where they came from. – Pav El Sep 12 '16 at 09:45
  • 1
    Possible duplicate of [remove row with nan value](http://stackoverflow.com/questions/5961839/remove-row-with-nan-value) or [here](http://stackoverflow.com/questions/15773189/remove-na-nan-inf-in-a-matrix) – Roman Sep 12 '16 at 10:00

3 Answers3

1

Perhaps you can try

na.omit(your.data.frame)

EDIT:

If you want to remove the entire column you can try

data[,the.number.of.the.column.with.NA] <- NULL  or
data$the.name.of.the.column.with.NA <- NULL
geo_dd
  • 283
  • 1
  • 5
  • 22
0
df[, colSums(is.na(df)) != nrow(df)]

from this answer

Community
  • 1
  • 1
Haboryme
  • 4,611
  • 2
  • 18
  • 21
0

to remove columns containing Na's or NaN's you can use this:

df <-  df[ , apply(df, MARGIN = 2, function(x) sum(is.na(x)) == 0)]
cccmir
  • 953
  • 6
  • 12