Dear Rui and Petr,
Thank you for taking time and effort to help.
Rui's solution is an effective workaround so that I can continue to
work with the data. However, the appearance of these NA rows (with NA
rownames) is clearly errorneous (possibly a bug behaviour due to R
base code). What I am interested is a solution that removes these NA
rows.
The reasons is because (1) prior to the NA assignment, one does not
need to test for NA value. (2) Besides, sometimes these NA values are
needed as part of the data to indicate that the missing data.
> t1[t1$Petal.Width==1.8, "Petal.Width"] <- NA
Petr's solution is also not apt in my case, because it removes 12 rows
that have NA values in "Petal.Width". I would like a solution that
keeps the 150 rows, but not the mysterious 12 rows with all NA values
in all columns.
Once again, I appreciate your suggestions and I am hoping that this
'errorneous' behaviour has a fix.
Cheers,
Ernest
On Mon, Jan 14, 2019 at 4:25 PM PIKAL Petr wrote:
>
> Hi
>
> If you want to remove rows with NA values from your data you could use
>
> ?complete.cases
>
> or
>
> t2 <- t1[!is.na(t1$Petal.Width),]
>
> Cheers
> Petr
>
> > -Original Message-
> > From: R-help On Behalf Of Rui Barradas
> > Sent: Saturday, January 12, 2019 12:55 PM
> > To: Ernest Han ; r-help@r-project.org
> > Subject: Re: [R] NA rows appeared in data.frame
> >
> > Hello,
> >
> > You have to test for NA. Some (12) of the values of t1$Petal.Width are NA
> > therefore t1$Petal.Width == 2.0 alone returns 12 NA values.
> >
> > t1[t1$Petal.Width == 2.0 & !is.na(t1$Petal.Width == 2.0), ]
> >
> > Or use which(t1$Petal.Width == 2.0).
> >
> > t1[which(t1$Petal.Width == 2.0), ]
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Às 08:23 de 12/01/2019, Ernest Han escreveu:
> > > 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.1NA NA NA NA
> > > NA.2NA NA NA NA
> > > NA.3NA NA NA NA
> > > 1116.5 3.2 5.1 2 virginica
> > > 1145.7 2.5 5.0 2 virginica
> > > NA.4NA NA NA NA
> > > 1225.6 2.8 4.9 2 virginica
> > > 1237.7 2.8 6.7 2 virginica
> > > NA.5NA NA NA NA
> > > NA.6NA NA NA NA
> > > NA.7NA NA NA NA
> > > NA.8NA NA NA NA
> > > 1327.9 3.8 6.4 2 virginica
> > > NA.9NA NA NA NA
> > > NA.10 NA NA NA NA
> > > 1486.5 3.0 5.2 2 virginica
> > > NA.11 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/Frameworks/
> > > vecLib.framework/Versions/A/libBLAS.dylib
> > > LAPACK:
> > > /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/
> > > vecLib.framework/Versions/A/libLAPACK.dylib
> > >
> > > locale:
> > > [1] en_US.UTF-8/en_US.UTF-8/en