Search code examples
rcountdataframeunique

Count unique value in one data frame where the unique value range is defined in another data frame


I have two different data frame below, Scale is the allowable score range/point and in this case is 1-5. Score is the actual score provided by participant using the value defined in Scale. I need count the number of score for each scale point in the Score Data frame. for example, there are two counts of 2 in Score,while zero count of 1.

a<-c(1,2,3,4,5)
b<- c(2,3,4,5,3,4,4,3,3,5,2,3,3)
Scale<-data.frame(Scale =a)
Score<-data.frame(Score=b)

I tried aggregate, but it only identify unique value it found in one data frame and couldn't consult with another one. For example, it will not be able to find there is zero score for 1 in Score and only return counts for 2,3,4,5.

Anyone has any good idea?


Solution

  • Like this?

    > table(factor(Score$Score, levels = a))
    
    1 2 3 4 5
    0 2 6 3 2