Getting R Frequency counts for all possible answers

后端 未结 2 805
忘了有多久
忘了有多久 2021-01-20 19:08

I\'ve started with R and I\'m still finding my way with syntax. I\'m looking to get the frequencies for a scaled variable which has values of 0 through 10 and NA.

         


        
2条回答
  •  心在旅途
    2021-01-20 19:45

    This kind of tasks is easily done with package dplyr. For keeping the non-used values of R, you have to define R as factor and use tidyr's complete-function

    library(dplyr)
    library(tidyr)
    df %>%
        mutate(R = factor(R, levels=1:10)) %>%
        group_by(R) %>%
        summarise(freq=n()) %>%
        complete(R, fill=list(freq=0))
    

提交回复
热议问题