Reputation: 60004
I have a data frame with several columns.
I want to select the rows with no NA
s (as with complete.cases
)
and all columns identical.
E.g., for
> f <- data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40))
> f
a b c
1 1 1 1
2 NA NA NA
3 NA 3 5
4 4 40 40
I want the vector TRUE,FALSE,FALSE,FALSE
selecting just the first row because there all 3 columns are the same and none is NA
.
I can do
Reduce("==",f[complete.cases(f),])
but that creates an intermediate data frame which I would love to avoid (to save memory).
Upvotes: 3
Views: 1027
Reputation: 60004
The best (IMO) solution is from David Winsemius:
which( rowSums(f==f[[1]]) == length(f) )
Upvotes: 0
Reputation: 2455
Try this:
R > index <- apply(f, 1, function(x) all(x==x[1]))
R > index
[1] TRUE NA NA FALSE
R > index[is.na(index)] <- FALSE
R > index
[1] TRUE FALSE FALSE FALSE
Upvotes: 1