问题描述
我有以下变量捕获的捕获数据:Year,Date,ID和Distance.moved:
> head(df)
Year ID Date Distance.moved
2012 2012-005 29-05-12 10
2012 2012-006 30-05-12 22
2013 2013-001 22-06-13 5
2013 2013-002 23-06-13 6
我想将0次出现添加到“移动的距离”列中,以获取不移动的事件的值,例如:
Year ID Date Distance.moved
2012 2012-005 29-05-12 10
2012 2012-005 30-05-12 0
2012 2012-006 29-05-12 0
2012 2012-006 30-05-12 22
2013 2013-001 22-06-13 5
2013 2013-002 22-06-13 0
2013 2013-002 23-06-13 6
2013 2013-001 23-06-13 0
因此,仅对“日期”列中作为事件存在的日期加上0,并将其每年分组。
我尝试为出现的情况添加二进制1,0列:
df_occ <- df %>%
group_by(Date,Year,ID) %>%
summarize(occurrence=n()) %>%
as.data.frame()
但这给我所有ID的出现值= 1
解决方法
按“年”分组后,我们可以使用complete
library(dplyr)
library(tidyr)
df1 %>%
group_by(Year) %>%
complete(ID,Date,fill = list(Distance.moved = 0))
# A tibble: 8 x 4
# Groups: Year [2]
# Year ID Date Distance.moved
# <int> <chr> <chr> <dbl>
#1 2012 2012-005 29-05-12 10
#2 2012 2012-005 30-05-12 0
#3 2012 2012-006 29-05-12 0
#4 2012 2012-006 30-05-12 22
#5 2013 2013-001 22-06-13 5
#6 2013 2013-001 23-06-13 0
#7 2013 2013-002 22-06-13 0
#8 2013 2013-002 23-06-13 6
数据
df1 <- structure(list(Year = c(2012L,2012L,2013L,2013L),ID = c("2012-005","2012-006","2013-001","2013-002"),Date = c("29-05-12","30-05-12","22-06-13","23-06-13"),Distance.moved = c(10L,22L,5L,6L)),class = "data.frame",row.names = c(NA,-4L))