如何使用dplyr
在R中执行此操作。我有这张桌子
Product Week Count
A Wk1 2
A Wk2 2
B Wk1 3
C Wk2 4
C Wk3 3
我希望输出为此
Product Week Count
A Wk1 2
A Wk2 2
A Wk3 0
B Wk1 3
B Wk2 0
B Wk3 0
C Wk1 0
C Wk2 4
C Wk3 3
答案 0 :(得分:1)
使用tidyverse
包,以下代码将完成您缺少的组合,允许您指定缺少组合的值:
library(tidyverse)
my_df %>%
complete(Product,Week,fill=list(Count=0))
结果如下:
# A tibble: 9 x 3
Product Week Count
<chr> <chr> <dbl>
1 A Wk1 2.00
2 A Wk2 2.00
3 A Wk3 0
4 B Wk1 3.00
5 B Wk2 0
6 B Wk3 0
7 C Wk1 0
8 C Wk2 4.00
9 C Wk3 3.00
答案 1 :(得分:0)
这是为缺少产品周组合添加零计数行的一种方法:
# 0. attach dplyr for join, mutate, and magrittr pipe (%>%)
library(dplyr)
# 1. get the data as a data frame
dat <- read.table(text="
Product Week Count
A Wk1 2
A Wk2 2
B Wk1 3
C Wk2 4
C Wk3 3
", header=TRUE)
# 2. get unique products and unique weeks
products <- unique(dat$Product)
weeks <- unique(dat$Week)
# 3.1 create a df with all combos of product and week
expand.grid(Product=products, Week=weeks, stringsAsFactors=FALSE) %>%
# 3.2 join it with original data (introduces NA counts for missing combos)
left_join(dat, by=c("Product", "Week")) %>%
# 3.3 replace NA counts with zero
mutate(Count = ifelse(is.na(Count), 0, Count))
确实有其他方法可以做到这一点,但是这样做(只要您在原始表格中永远不会有NA
个值。)