以下是数据:
# vector1 dates
reading.dates <- as.Date(c("2012-02-13","2012-02-20","2012-02-28"))
mydat <- data.frame (ID = c("A", "B", "C", "D"), Date1 = c(1:4),
Date2 = c(5:8), Date3 = c(11:14))
mydat
ID Date1 Date2 Date3
1 A 1 5 11
2 B 2 6 12
3 C 3 7 13
4 D 4 8 14
这是我想要做的:
每个ID级别,
在date1和date2之间
计算差异是日期 -
reading.dates[2] - reading.dates[1] = 7 = say "Y"
添加相应的日期值和平均值 -
(date1 + date2) / 2 = say "X"
then multiply = X * Y
for indiviudual A:((1 + 5) / 2)* 7 )
在date2和date3之间
类似于date2和date3 ....所以在文件的末尾,实际上我有更多的变量
reading.dates [3] - reading.dates [2] 时差8天
表示个人(5 + 11)/ 2)* 8
然后添加所有值。
对于A,它将是
((1 + 5) / 2)* 7 ) + ((5+11)/2) * 8)
由于
答案 0 :(得分:1)
尝试使用此代码,我认为它适用于任何大小的data.frame和日期向量:
calculateData <- function(dates, data)
{
if (length(dates) != (ncol(data)-1))
stop("The columns and dates doesn't match")
out <- as.data.frame(NULL)
for (i in 1:nrow(data))
{
x <- NULL
for (j in 2:length(data))
x <- c(x, (data[i,j]+data[i,j+1])/2 * difftime(dates[j], dates[j-1], "days"))
out <- rbind(out, c(data[i,1], sum(x)))
}
colnames(out) <- c("ID", "VAL")
return(out);
}
reading.dates <- as.Date(c("2012-02-13","2012-02-20","2012-02-28"))
mydat <- data.frame (ID = c("A", "B", "C", "D"), Date1 = c(1:4), Date2 = c(5:8), Date3 = c(11:14))
cd <- calculateData(reading.dates, mydat)
print(cd)
您可以自行更改返回数据的方式。
> print(cd)
ID VAL
1 1 85
2 2 100
3 3 115
4 4 130