[R] Help with finding mean at 1 second interval

Jim Lemon jim at bitwrit.com.au
Tue Mar 18 23:47:01 CET 2014


On 03/19/2014 02:26 AM, Satish Anupindi Rao wrote:
> Hi,
> ...
> I would like to find the mean of the V2 to V6 columns on a per second interval. Would anyone please be able to help me with a function and implementation for that please?
>

Hi Satish,
If you don't care about the location of the intervals, try this:

sardat<-read.table(text="V0 V1 V2 V3 V4 V5 V6
2014-03-14 22:41:46.988804  10   2   8   3  14
2014-03-14 22:41:46.991126  13   4   9   5  15
2014-03-14 22:41:46.993506  12   4   8   3  14
2014-03-14 22:41:46.993755  19   4  15   5  22
2014-03-14 22:41:46.997780  21   5  16   7  24
2014-03-14 22:41:47.000154  18   5  13   3  21
2014-03-14 22:41:47.002376  21   5  16   6  23
2014-03-14 22:41:47.011106  12   4   8   3  14
2014-03-14 22:41:47.012691  12   4   8   3  16
2014-03-14 22:41:47.017579  11   2   9   3  12
2014-03-14 22:41:47.019463  12   5   7   3  15
2014-03-14 22:41:47.020247  14   6   8   3  17",
header=TRUE,stringsAsFactors=FALSE)

getsec<-function(x) {
  unlist(strsplit(unlist(strsplit(x,":"))[3],"[.]"))[1]
}

sardat$sec<-sapply(sardat$V1,getsec)

sapply(sardat[,c("V2","V3","V4","V5","V6")],by,sardat$sec,mean)

Jim




More information about the R-help mailing list