Table by row with R

前端 未结 4 1474
暗喜
暗喜 2021-01-22 04:19

I would like to tabulate by row within a data frame. I can obtain adequate results using table within apply in the following example:

         


        
4条回答
  •  故里飘歌
    2021-01-22 04:47

    I think the problem is stated in applys help:

    ... If n equals 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim(X)[MARGIN] otherwise ...

    The inconsistencies of the return values of base R's apply family is the reason why I shifted completely to plyrs **ply functions. So this works as desired:

    library(plyr)
    alply( df.2[ 4:8 ], 1, function(x) table( unlist(x), useNA = "ifany" ) )
    

提交回复
热议问题