I've a dataset (teleco) with 3000 obs and there are many (332) missing value (NA)s in it. When i try to subset all the missing values into another dataframe, I strangely endup with 3745 obs.
clean = na.omit(teleco)
new = teleco[is.na(teleco[1:19])==TRUE, ]
clean - 2668 obs of 19 vars
new - 3745 obs of 19 vars
teleco - 3000 obs of 19 vars
I'm sure that i'm doing something wrong. Can someone please help?
If we need to subset rows having at least one NA
, we can create an index using rowSums
on the logical matrix (is.na(teleco)
), and convert that to a logical vector (!=0
).
teleco[rowSums(is.na(teleco))!=0,]
Or we can use apply
with MARGIN=1
to create a logical vector.
teleco[apply(is.na(teleco), 1, any),]
set.seed(24)
teleco <- as.data.frame(matrix(sample(c(NA,0:10), 20*5, replace=TRUE), ncol=5))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments