Home > Blockchain >  Check value of one column based on another R
Check value of one column based on another R

Time:06-15

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).

CodePudding user response:

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

CodePudding user response:

would this help ?

> df %>% count(Name, Score) %>% filter(n<2)
  Name Score n
1 Tina    26 1
2 Tina    78 1
  • Related