Proper idiom for adding zero count rows in tidyr/dplyr

后端 未结 5 1012
旧巷少年郎
旧巷少年郎 2020-11-27 16:02

Suppose I have some count data that looks like this:

library(tidyr)
library(dplyr)

X.raw <- data.frame(
    x = as.factor(c(\"A\", \"A\", \"A\", \"B\", \         


        
5条回答
  •  日久生厌
    2020-11-27 16:49

    You can use tidyr's expand to make all combinations of levels of factors, and then left_join:

    X.tidy %>% expand(x, y) %>% left_join(X.tidy)
    
    # Joining by: c("x", "y")
    # Source: local data frame [4 x 3]
    # 
    #   x  y count
    # 1 A  i     1
    # 2 A ii     5
    # 3 B  i    15
    # 4 B ii    NA
    

    Then you may keep values as NAs or replace them with 0 or any other value. That way isn't a complete solution of the problem too, but it's faster and more RAM-friendly than spread & gather.

提交回复
热议问题