简体   繁体   中英

Check value of one column based on another R

Say I have a dataframe

Name <- c("Jon", "Jon", "Maria", "Maria", "Tina", "Tina")
Score <- c(23, 23, 32, 32, 26, 78)
df <- data.frame(Name, Score)

I would like to see if the Score column is the same or different per name. In theory, I expect the score for each column to be the same per name, but it could be the case that they're different (like with Tina) and I would like to check.

What might be an efficient way to do this? (My dataframe has over 150 000 rows).

Try this to get the counts, then you can check if Name is duplicated

library(magrittr)
library(dplyr)
df %>%
  count(Name, Score)%>%
  add_count(Name, name = "name_n")%>%
  filter(name_n > 1)

#output
  Name Score n name_n
1 Tina    26 1      2
2 Tina    78 1      2

would this help ?

> df %>% count(Name, Score) %>% filter(n<2)
  Name Score n
1 Tina    26 1
2 Tina    78 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