我想计算 a 行中元素之间的平均时间data.frame
。
> x <- structure(list(`as.Date("2010-12-31")` = structure(c(14974, 14974,
14974, 14974, 14974), class = "Date"), Date1_P2 = structure(c(14061,
11566, 11747, 13848, 12965), class = "Date"), Date2_P2 = structure(c(NA,
10408, 11627, 10074, 6329), class = "Date"), Date3_P2 = structure(c(NA,
8370, 11566, NA, NA), class = "Date")), .Names = c("as.Date(\"2010-12-31\")",
"Date1_P2", "Date2_P2", "Date3_P2"), row.names = c("0000001.1",
"0000004.2", "0000005.2", "0000009.3", "0000010.1"), class = "data.frame")
> x
as.Date("2010-12-31") Date1_P2 Date2_P2 Date3_P2
0000001.1 2010-12-31 2008-07-01 <NA> <NA>
0000004.2 2010-12-31 2001-09-01 1998-07-01 1992-12-01
0000005.2 2010-12-31 2002-03-01 2001-11-01 2001-09-01
0000009.3 2010-12-31 2007-12-01 1997-08-01 <NA>
0000010.1 2010-12-31 2005-07-01 1987-05-01 <NA>
我写了一个函数来计算每一行。
> avgtime <- function(history){
difftime <- vector("numeric", length=9)
i <- 2
while(!is.na(history[i]) & i < 4){
difftime[i-1] <- history[i-1] - history[i]
i <- i + 1
}
return(mean((unlist(difftime[which(difftime!=0)]))))
}
> for(i in 1:nrow(x)){print(avgtime(x[i,]))}
[1] 913
[1] 2283
[1] 1673.5
[1] 2450
[1] 4322.5
但是当我尝试apply
这样做时data.frame
,我遇到了问题。
> apply(x, 1, avgtime)
Error in history[i - 1] - history[i] :
non-numeric argument to binary operator
什么叫更合适apply
?