[英]R - trying to Group a data frame by a factor but capture the count of occurrences of that factor
[英]R - Add 0 occurrences in capture-recapture data
我有带有变量 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.