我有一个数据框。 我想找出某个地区未记录哪些疾病。 因此,例如: A区没有腮腺炎
我想做的是在某个区域没有疾病的地方,我想在n列中记录一个零。
我认为可能是
DATA$missing<-DATA%>% if (DISEASE %in% DISEASE){"no"}
但是那是行不通的,我没想到它会,但是希望它会...
这是我的数据,在此示例中,我通过过滤原始测试数据帧从区域A中删除了流行性腮腺炎。 如何确定该新数据集中的区域中没有流行性腮腺炎?以及可能缺少的疾病和区域的其他组合,然后在计数列中返回n = 0的值? 谢谢。
library (tidyverse)
library (epitools)
# here's my made up data
DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")
DATA = data.frame(DISEASE, YEAR, VALUE,AREA)
DATA<-DATA%>%filter(DISEASE !="Mumps" | AREA !="A")
编辑: 我的预期结果是这样
new_row<-c("Mumps","2015",0,"A")
DATA<-rbind(DATA,new_row)
答案 0 :(得分:3)
DATA %>% complete(AREA, DISEASE, fill=list(VALUE=0))
# A tibble: 14 x 4
# Groups: AREA [3]
AREA DISEASE YEAR VALUE
<fct> <fct> <dbl> <dbl>
1 A Chicky Pox 2013 95
2 A Marco Polio 2011 82
3 A Marco Polio 2014 51
4 A Mumps NA 0
5 B Chicky Pox 2011 71
6 B Chicky Pox 2014 61
7 B Marco Polio 2012 89
8 B Marco Polio 2015 51
9 B Mumps 2013 69
10 C Chicky Pox 2012 69
11 C Chicky Pox 2015 87
12 C Marco Polio 2013 79
13 C Mumps 2011 79
14 C Mumps 2014 89
如果您希望0行具有特定年份,可以将其添加到fill=list()
参数中。