Skip to content
Prev 378195 / 398502 Next

NA rows appeared in data.frame

Hi

You put NA to some variable in 150 rows. So you do not have "mysterious" NA rows in your file. If you want to select anything based on column with NA values you have to perform your selection using which (as Rui suggested).

It is documented in help page, although it is probably rather less comprehensible (maybe some example added to help page could be useful).
-----
NAs in indexing

When extracting, a numerical, logical or character NA index picks an unknown element and so returns NA in the corresponding element of a logical, integer, numeric, complex or character result, and NULL for a list. (It returns 00 for a raw result.)
-----
I believe that this behaviour has some reason, because you compare 2 to NA and NA is basically "I do not know". So it could be 2 and therefore also rows with NA are returned. If I am wrong, I hope R gurus will correct me.

You said you want to remove rows with NA values, therefore I suggested complete.cases function. After this you end with object stripped from rows with NA values so with less rows.

I would be rather cautious with word "errorneous". I remember old days when Excel considered empty cells as zeros and gave "errorneous" calculations but I believe that it was pretty sensible from accountant point of view as empty cell means 0.

In almost all cases, analysis in R give you correct results, you just need to tell R how to apply function to object with NA values.
[1] NA
[1] 1.147101
Cheers
Petr
Now I am puzzled what do you really want?

with your example and my suggestion you get

t1 <- iris
t1[t1$Petal.Width==1.8, "Petal.Width"] <- NA
t2 <- t1[!is.na(t1$Petal.Width),]
t2[t2$Petal.Width == 2.0, ]
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
111          6.5         3.2          5.1           2 virginica
114          5.7         2.5          5.0           2 virginica
122          5.6         2.8          4.9           2 virginica
123          7.7         2.8          6.7           2 virginica
132          7.9         3.8          6.4           2 virginica
148          6.5         3.0          5.2           2 virginica
[1] 138   5
[1] 150   5
Osobn? ?daje: Informace o zpracov?n? a ochran? osobn?ch ?daj? obchodn?ch partner? PRECHEZA a.s. jsou zve?ejn?ny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner?s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
D?v?rnost: Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a podl?haj? tomuto pr?vn? z?vazn?mu prohl??en? o vylou?en? odpov?dnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/