Select rows with same columns from dataframe

I have a data frame with multiple columns. I want to select rows without NA

(like with complete.cases

) and all columns are identical. For example, 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 to TRUE,FALSE,FALSE,FALSE

only select the first row because all three columns are the same and none of them are NA

.

I can do

Reduce("==",f[complete.cases(f),])

      

but this creates an intermediate dataframe which I would like to avoid (to save memory).

+3


source to share


2 answers


Try the following:



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

      

+1


source


Best Solution (IMO) - David Winsemius :



which( rowSums(f==f[[1]]) == length(f) )

      

0


source







All Articles