[R] NA rows appeared in data.frame

S Ellison S@Elli@on @ending from LGCGroup@com
Mon Jan 14 11:51:40 CET 2019


> After replacing some values in a data.frame, NAs rows have appeared
> and cannot be removed. 
I'm not clear why you say 'cannot be removed', which sounds quite a bit stronger than 'I couldn't ...'. 
The example you gave returned new NA rows because your logical test included NAs (Petal.Width == 2.0 returns NA for all of the NA petal widths, and an NA in indexing returns an NA row). 
But 'cannot be removed' sounded to me as if you've read somewhere that it's impossible, or that you've tried something that should work and didn't; if you meant either of those you'll have to say what the problem was.

In the mean time: 
If you want to remove rows containing _any_ NAs, see ?complete.cases  and use something like
t1[complete.cases(t1),]

If you want to remove rows that are _all_ NA, you may need something like

subset(t1, apply(t1, 1, function(x) !all(is.na(x))))
(or the equivalent '[' usage)

and, as an aside, using '==' for floating point numbers is not generally safe; for example
> sqrt(2)^2 == 2.0
[1] FALSE

See R FAQ 7.31 for details of why '==' is bad for floating point, if you haven't already.


S Ellison

> -----Original Message-----
> From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Ernest Han
> Sent: 12 January 2019 08:23
> To: r-help using r-project.org
> Subject: [R] NA rows appeared in data.frame
> 
> Dear All,
> 
> After replacing some values in a data.frame, NAs rows have appeared
> and cannot be removed. I have googled these issues and found that
> several people have encountered it. Solutions in stackoverflow seem to
> provide work-arounds but does not remove it from the data.frame.
> Therefore, I am turning to experts in this community for help.
> 
> The code is as follows,
> 
> > t1 <- iris
> > t1[t1$Petal.Width==1.8, "Petal.Width"] <- NA
> > t1[t1$Petal.Width == 2.0, ]
>       Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
> NA              NA          NA           NA          NA      <NA>
> NA.1            NA          NA           NA          NA      <NA>
> NA.2            NA          NA           NA          NA      <NA>
> NA.3            NA          NA           NA          NA      <NA>
> 111            6.5         3.2          5.1           2 virginica
> 114            5.7         2.5          5.0           2 virginica
> NA.4            NA          NA           NA          NA      <NA>
> 122            5.6         2.8          4.9           2 virginica
> 123            7.7         2.8          6.7           2 virginica
> NA.5            NA          NA           NA          NA      <NA>
> NA.6            NA          NA           NA          NA      <NA>
> NA.7            NA          NA           NA          NA      <NA>
> NA.8            NA          NA           NA          NA      <NA>
> 132            7.9         3.8          6.4           2 virginica
> NA.9            NA          NA           NA          NA      <NA>
> NA.10           NA          NA           NA          NA      <NA>
> 148            6.5         3.0          5.2           2 virginica
> NA.11           NA          NA           NA          NA      <NA>
> 
> ## Twelve values were replaced, twelve NA rows appeared.
> 
> ### MISC INFO ###
> > sessionInfo()
> R version 3.4.0 (2017-04-21)
> Platform: x86_64-apple-darwin16.5.0 (64-bit)
> Running under: macOS  10.14.2
> 
> Matrix products: default
> BLAS:
> /System/Library/Frameworks/Accelerate.framework/Versions/A/Framewor
> ks/vecLib.framework/Versions/A/libBLAS.dylib
> LAPACK:
> /System/Library/Frameworks/Accelerate.framework/Versions/A/Framewor
> ks/vecLib.framework/Versions/A/libLAPACK.dylib
> 
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.4.0 tools_3.4.0
> > Sys.getlocale()
> [1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-
> 8"
> 
> 
> Thank you,
> Ernest
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list