4

I'm selecting data for analysis and need to select individuals (cows) that appear in multiple groups (herds). Individuals appear multiple times in my data set as I have multiple measurements. My data simply looks like:

data <- data.frame( 
cow = c("cow1", "cow1", "cow2", "cow2", "cow3"), #character variable
herd = c(1, 2, 1, 1, 1)) # factor variable

So I'd like to filter cow1, as it appears in herd1 and herd2. So cows that only appear once in the data set or only appear in one herd should be removed.

I've tried the group_by and filter functions, but none seem to work.

data <- data %>%
group_by(cow, herd) %>%
filter(n() > 1) %>%
ungroup()

However, this does not give the needed result. I'm kind of lost at the moment, so I hope someone can help me out on this.

smci
  • 32,567
  • 20
  • 113
  • 146
Vera
  • 57
  • 5

1 Answers1

5

How about:

library(dplyr)

data %>%
  group_by(cow) %>%
  filter(n_distinct(herd) > 1)
Aron Strandberg
  • 3,040
  • 9
  • 15