将两个数据帧有条件地与日期组合

时间:2017-07-13 05:22:10

标签: r date if-statement dataframe count

我正在使用两个不同的数据帧。我想使用另一个中包含的信息来完成一个。第一个数据框包含已知出生日和出生地区的个体幼小动物的观察清单。第二个数据框包含在给定时间间隔内存在于给定地区的成年动物的观察结果。 这是一个可重复的例子:

#First dataframe:
ID_young <- c(rep(c("a", "b", "c"), each=3), "d") # All individuals observed three times except "d", observed once
Territory_young <- c(rep(c("x", "y", "z"), each=3), "x") # All individuals are from different territories, except "a" and "d" who are from the same territory, namely "x".
Birthdate <- c(rep(c("2014-01-29", "2014-12-17", "2013-11-19"), each=3), "2012-12-04")
Birthdate <- as.Date(Birthdate)

# Second dataframe:
ID_adult <- c("e", "f", "g", "h", "i", "j", "e","f")
Territory_adult <- c("x", "x", "y", "z", "z", "z", "z", "w")
First_date <- as.Date(c("2014-01-01", "2014-01-15", "2013-12-14", "2013-05-17", "2013-05-09", "2012-09-01", "2013-06-18", "2011-04-17"))
Last_date <- as.Date(c("2014-02-28", "2014-04-17", "2014-11-02", "2014-01-13", "2015-01-03", "2013-04-17", "2013-12-25", "2014-11-11"))

# Data frames complete:
df1 <- data.frame(ID_young, Territory_young, Birthdate)
df2 <- data.frame(ID_adult, Territory_adult, First_date, Last_date)

我的目标是在df1中创建一个新列,其中包含出生时幼小动物领域中存在的成年动物的数量。 换句话说,

对于df1的每一行:

  • 在df2
  • 中找到相应的地区
  • 计算df2中的行数,其中df2 $ First_date和df2 $ Last_date之间的间隔包括df1 $ Birthdate
  • 在df1
  • 的新栏中填写该号码

例如,对于df1的前三行(对应于年轻的动物&#34; a&#34;),该计数将为2,因为成年人&#34; e&#34;和&#34; f&#34;在领土上出现&#34; x&#34;年轻的时候&#34; a&#34;出生(2014-01-29)。

有人可以帮助我制定条件陈述的正确组合,这样我就能做到吗?我正在努力,如果当前的陈述,但没有任何值得展示。

谢谢!

2 个答案:

答案 0 :(得分:1)

parent

答案 1 :(得分:1)

data.table的最新版本支持非等联接,可用于此目的:

library(data.table)   # CRAN version 1.10.4 used
# coerce to data.table
DT1 <- data.table(df1)
DT2 <- data.table(df2)

# right non-equi join to find any adults present in terrority during birth
DT2[unique(DT1), 
    on = c("Territory_adult==Territory_young", 
           "First_date<=Birthdate",
           "Last_date>=Birthdate")][
             # count adults for each young
             , .(Count_adult = sum(!is.na(ID_adult))), by = ID_young][
             # join counts into each matching row of first data.table
               DT1, on = "ID_young"]
    ID_young Count_adult Territory_young  Birthdate
 1:        a           2               x 2014-01-29
 2:        a           2               x 2014-01-29
 3:        a           2               x 2014-01-29
 4:        b           0               y 2014-12-17
 5:        b           0               y 2014-12-17
 6:        b           0               y 2014-12-17
 7:        c           3               z 2013-11-19
 8:        c           3               z 2013-11-19
 9:        c           3               z 2013-11-19
10:        d           0               x 2012-12-04

请注意,df1DT1,包含重复的行,需要在与成人的非equi联接中使用unique(),并最终使用其他联接以确保成人数出现在每排。

相关问题