我想将poststamp-1的所有元素与datestamp-2的第一个元素相加后求和,然后对datestamp-2的第二个元素重复相同的操作,以此类推,在R中
datestamp1 datestamp2 load_percent
2019-05-28 2019-05-25 0.01883
2019-05-29 2019-05-26 0.72340
预期结果如下。
datestamp1 datestamp2 weighted_index
2019-05-28 2019-05-25 2.95
2019-05-29 2019-05-26 2.20
逻辑存在
1. 2019-05-28 - 2019-05-25 = 3 * 0.01 = 0.056
2. 2019-05-29 - 2019-05-25 = 4 * 0.72 = 2.893
0.056和2.893的总和是2.95
类似
3. 2019-05-28 - 2019-05-26 = 2 * 0.01 = 0.037
4. 2019-05-29 - 2019-05-26 = 3 * 0.72 = 2.170
0.037和2.170之和为2.20
答案 0 :(得分:0)
您可以使用sapply
df$weighted_index <- sapply(df$datestamp2, function(x)
sum((df$datestamp1 - x) * df$load_percent))
df
# datestamp1 datestamp2 load_percent weighted_index
#1 2019-05-28 2019-05-25 0.01883 2.95009
#2 2019-05-29 2019-05-26 0.72340 2.20786
请确保您的列datestamp1
和datestamp2
属于Date
类。如果不是,请先将它们转换为日期,然后使用上面的代码。
df[1:2] <- lapply(df[1:2], as.Date)
答案 1 :(得分:0)
我们可以使用outer
来获取两个日期列之间的差,然后使用'load_percent'来获取产品的colSums
df1$weighted_index <- with(df1, colSums(outer(datestamp1,
datestamp2, FUN = `-` ) * load_percent))
df1
# datestamp1 datestamp2 load_percent weighted_index
#1 2019-05-28 2019-05-25 0.01883 2.95009
#2 2019-05-29 2019-05-26 0.72340 2.20786
df1 <- structure(list(datestamp1 = structure(c(18044, 18045), class = "Date"),
datestamp2 = structure(c(18041, 18042), class = "Date"),
load_percent = c(0.01883, 0.7234)), row.names = c(NA, -2L
), class = "data.frame")