使用R中其他列的相应值查找最大数据

时间:2012-05-23 04:24:38

标签: r matrix

我有每日数据(4011天)和指标(1个工作日,2个周末)。我想找到每周最大值与相应的指标。例如(比方说)数据是:

mydat <- matrix(c(0.027,0.034,0.019,0.021,0.026,0.024,0.058,0.026,0.064,
     0.066,0.026,0.101,0.069,0.054,rep(2,2),rep(1,5),rep(2,2),rep(1,5)), ncol=2)

我尝试使用以下代码。我设法获得最大序列(在这种情况下,每周最大值),但我不想要指标中的最大序列。这是代码

week.max <- function(vec){    
   if(length(vec[is.na(vec)]) == 7){       
   return(NA)     
   } 
   else{
     return(max(vec, na.rm = T))
   } 
}

max.week.dat <- apply(mydat, 2, function(x) tapply(x, rep(1:(length(x)/7), 
  each=7, len=length(x)), week.max)) 

和结果

matrix(c(0.058,0.101,2,2),ncol=2)

我希望输出如下:

matrix(c(0.058,0.101,1,1),ncol=2)

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

这是数据(第三周额外的一天)

mydat <- data.frame(value = c(0.027,0.034,0.019,0.021,0.026,0.024,0.058,0.026,0.064,
     0.066,0.026,0.101,0.069,0.054,0.95), ind = c(rep(2,2),rep(1,5),rep(2,2),rep(1,5),2))

你的功能

week.max <- function(vec){    
  if(length(vec[is.na(vec)]) == 7){       
return(NA)     
 } 
 else{
return(max(vec, na.rm = T))
 } 
}

添加周信息

mydat$week <- c(rep(1:2,each=7),3)

使用与here

相同的解决方案
library(plyr)
ddply(mydat, .(week), subset, subset = value==week.max(value), select = -week)