我的数据框由对个体动物的个体观察组成。每只动物都有一个生日,我想将其与日期向量中最接近的野外季节日期相关联。
这是一个非常基本的可重现示例:
ID <- c("a", "b", "c", "d", "a") # individual "a" is measured twice here
birthdate <- as.Date(c("2012-06-12", "2014-06-14", "2015-11-11", "2016-09-30", "2012-06-12"))
df <- data.frame(ID, birthdate)
# This is the date vector
season_enddates <- as.Date(c("2011-11-10", "2012-11-28", "2013-11-29", "2014-11-26", "2015-11-16", "2016-11-22", "2012-06-21", "2013-06-23", "2014-06-25", "2015-06-08", "2016-06-14"))
Run Code Online (Sandbox Code Playgroud)
使用以下代码,我可以获得出生日期和最接近的季节结束日期之间的差异。
for(i in 1:length(df$birthdate)){
df$birthseason[i] <- which(abs(season_enddates-df$birthdate[i]) == min(abs(season_enddates-df$birthdate[i])))
}
Run Code Online (Sandbox Code Playgroud)
但是,我想要的是实际日期,而不是差异。例如,birthseason 的第一个值应该是 2012-06-21。
我正在尝试使用grid.arrange()
和 来编译四个图表,以减少每个图的边距,以便它们美观且紧凑。我想使用theme(plot.margin=unit(c(x, x, x , x), "cm"))
(欢迎其他解决方案)。
不久前有人问过类似的问题: here
但是,现在需要没有默认值的plot.margin
参数。units
我找不到任何关于 R 在这个论证中期望什么的解释。有人能举个例子吗?
如需可重现的示例,请使用旧问题中提供的示例。谢谢!