rcountfind-occurrences

Count occurrences of value in a set of variables in R (per row)


Let's say I have a data frame with 10 numeric variables V1-V10 (columns) and multiple rows (cases).

What I would like R to do is: For each case, give me the number of occurrences of a certain value in a set of variables.

For example the number of occurrences of the numeric value 99 in that single row for V2, V3, V6, which obviously has a minimum of 0 (none of the three have the value 99) and a maximum of 3 (all of the three have the value 99).

I am really looking for an equivalent to the SPSS function COUNT: "COUNT creates a numeric variable that, for each case, counts the occurrences of the same value (or list of values) across a list of variables."

I thought about table() and library plyr's count(), but I cannot really figure it out. Vectorized computation preferred. Thanks a lot!


Solution

  • Try

    apply(df,MARGIN=1,table)
    

    Where df is your data.frame. This will return a list of the same length of the amount of rows in your data.frame. Each item of the list corresponds to a row of the data.frame (in the same order), and it is a table where the content is the number of occurrences and the names are the corresponding values.

    For instance:

    df=data.frame(V1=c(10,20,10,20),V2=c(20,30,20,30),V3=c(20,10,20,10))
    #create a data.frame containing some data
    df #show the data.frame
      V1 V2 V3
    1 10 20 20
    2 20 30 10
    3 10 20 20
    4 20 30 10
    apply(df,MARGIN=1,table) #apply the function table on each row (MARGIN=1)
    [[1]]
    
    10 20 
     1  2 
    
    [[2]]
    
    10 20 30 
     1  1  1 
    
    [[3]]
    
    10 20 
     1  2 
    
    [[4]]
    
    10 20 30 
     1  1  1 
    
    #desired result