简体   繁体   中英

R keep only rows with a unique value in a certain column

I don't think this exact question has been asked since I'm not exactly trying to deduplicate. I have a data frame with several columns. One column is NAME. I want to extract only rows whose value for NAME is unique. For instance if I have:

NAME    V2     V3
John    9      A
John    8      B
Alex    9      A
Bret    9      A

Then I'd like to get:

NAME    V2     V3
Alex    9      A
Bret    9      A

Thanks...

There are multiple ways you could accomplish this. One is:

df<-data.frame(NAME=c("John", "John","Alex", "Brett"), V2=c(9,8,9,9), V3=c("A","B","A","A"))


df[!df$NAME %in% names(which(table(df$NAME)>1)),]

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