简体   繁体   中英

Return names after comparing many vectors in R

    Col1  Col2  Col3  Col4
R1  A     B     B     A
R2  D     D     NA    D
R3  E     E     E     E
R4  K     NA    NA   NA
R5  U     U     U    NA
R6  Z     Y     Y    Y

I would like to return the names of {R2,R3,R4,R5} because all the columns match within the row or are NA. In the real dataset there will be hundreds of columns.

I have found this piece of code semi-useful (let's say this is a data frame called 'dat'):

all.equal(dat[5,],dat[6,])

But that only returns the columns are mismatched rows (and it only does it for 2 rows).

rownames(dat)[apply(dat, 1, function(x) length(unique(na.omit(x)))) == 1]
[1] "R2" "R3" "R4" "R5"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM