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

后端 未结 5 1753
-上瘾入骨i
-上瘾入骨i 2021-01-06 01:16

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

5条回答
  •  青春惊慌失措
    2021-01-06 01:43

    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
    

提交回复
热议问题