0

The given data is like this:

df<-data.frame(farmer=c("F1","F1","F1","F2","F2","F2","F3","F4","F4"),
               c2=c(4,4,5,3,3,3,1,2,1))
df

img of the data Question is: I would like to get the sum of farmer whose values in the c2 column are the same.

The output of this problem is 2. But how to use code to realize it? Which function should be used?

ezio4df
  • 3,541
  • 6
  • 16
  • 31

1 Answers1

0

You can try the code below

nrow(unique(subset(df,ave(c2,farmer)==c2)))

where subset+ ave filters out the farmers who have identical c2 values, and nrow + unique helps count the number of those farmers.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81