Counting observations according to the number of variables missing

前端 未结 2 619
鱼传尺愫
鱼传尺愫 2021-01-15 06:43

I would like to count the rows of a data frame, according to the number of variables that are missing. So for example in the data frame below I would like the code to return

2条回答
  •  孤城傲影
    2021-01-15 06:58

    table(rowSums(is.na(dt)))
    #0 1 2 3 
    #3 5 1 1 
    

    If you really need the last 0 (four NAs):

    tabulate(factor(rowSums(is.na(dt))), nbins = ncol(dt)+1)
    #[1] 3 5 1 1 0
    

提交回复
热议问题