[R] NA in logical vector = data frame row numbers scrambled

Petr Pikal petr.pikal at precheza.cz
Mon Apr 14 11:51:44 CEST 2003


Dear all.

RE how to estimate parameters of multimodal distribution
Thank to prof.Ripley for pointing me to mclust package, although I am not sure I 
can apply it to my problem.

I have another question. 

I need to change some of my values in data frame to NA.

I use something like  
df[df$v1 < 5, 5:10] <- NA 

which is OK if there are no NA values in v1.

here are some foo attempts 
> test
   index cislo     time den hod min  zatizdp   plyndp  skalice
5      5     1 37693.79  13  19   0 106.6707 533.0288 5.932448
6      6     1 37693.80  13  19  15 106.2308 533.8799 6.008640
7      7     1 37693.81  13  19  30 106.3643 534.5321 5.960807
8      8     1 37693.82  13  19  45 106.9483 533.9640 5.962759
9      9     1 37693.83  13  20   0 106.9289 533.9978 5.939210
10    10     1 37693.84  13  20  15 107.1585 518.3881 5.980370

> test[test$min==0,7:9]<-NA

> test
   index cislo     time den hod min  zatizdp   plyndp  skalice
5      5     1 37693.79  13  19   0       NA       NA       NA
6      6     1 37693.80  13  19  15 106.2308 533.8799 6.008640
7      7     1 37693.81  13  19  30 106.3643 534.5321 5.960807
8      8     1 37693.82  13  19  45 106.9483 533.9640 5.962759
9      9     1 37693.83  13  20   0       NA       NA       NA
10    10     1 37693.84  13  20  15 107.1585 518.3881 5.980370

but further on

> test[test$plyndp<520,7:9]<-NA
Error in if (all(i >= 0) && (nn <- max(i)) > nrows) { : 
        missing value where logical needed

the problem is in logical vector having NA

> test$plyndp<520
[1]    NA FALSE FALSE FALSE    NA  TRUE

and subsequent scrambled row numbering

> test[test$plyndp<520,7:9]
     zatizdp   plyndp skalice
NA        NA       NA      NA
NA1       NA       NA      NA
X10 107.1585 518.3881 5.98037

Is there some more simple or direct way how to achieve this

> test[complete.cases(test),][na.omit(test$plyn<530),]
   index cislo     time den hod min  zatizdp   plyndp skalice
10    10     1 37693.84  13  20  15 107.1585 518.3881 5.98037

to be able to change values in data frame gradually to NA?

Excuse my awkward English. I hope you will understand my problem.
Thanks.

Best regards.

Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz



More information about the R-help mailing list