我一直在努力解决这个问题,并决定在一些失败之后寻求一些帮助.
这是我的问题,我想根据当天划分这两个向量,例如2012-12-11将是3/17而2012-12-12应该是0/7.但是我似乎无法弄清楚如何做到这一点..
> ili
2012-12-11 2012-12-13 2012-12-14 2012-12-17
3 6 7 1
> no.ili
2012-12-11 2012-12-12 2012-12-13 2012-12-14 2012-12-15 2012-12-16 2012-12-17
17 7 232 322 38 21 36
Run Code Online (Sandbox Code Playgroud)
最后的尝试是循环遍历两个向量并将值或零添加到新向量但是当我使用%in%它时不会按顺序放置值(显然)但是如果我使用==它也不起作用..
days.ili <- unique(one.three$timestamp)
days <- unique(one.week$timestamp)
ili.vec <- rep(0, length(days))
for (i in 1:length(days)) {
if (days.ili[i] %in% days) {
ili.vec[i] <- ili[i]
} else {
ili.vec[i] <- 0
}
}
Run Code Online (Sandbox Code Playgroud)
我必须忘记一些事情,因为我无法看清这个问题.任何人都可以告诉我在R中实现这个目标的最佳方法吗?
也许一个选项将使用merge..
Rom*_*ois 11
像这样的东西:
res <- rep(0, length(no.ili))
where <- match( names(ili), names(no.ili) )
res[ where ] <- ili / no.ili[where]
names(res) <- names(no.ili)
res
# 2012-12-11 2012-12-12 2012-12-13 2012-12-14 2012-12-15 2012-12-16 2012-12-17
# 0.17647059 0.00000000 0.02586207 0.02173913 0.00000000 0.00000000 0.02777778
Run Code Online (Sandbox Code Playgroud)