[R] Help with finding mean at 1 second interval

arun smartpink111 at yahoo.com
Wed Mar 19 00:48:46 CET 2014


Hi,
You could also try:
library(plyr)

ddply(mutate(sardat,sec=as.numeric(gsub(".*:(.*)\\..*$","\\1",sardat$V1))),.(sec), numcolwise(mean))
#  sec       V2       V3        V4       V5       V6
#1  46 15.00000 3.800000 11.200000 4.600000 17.80000
#2  47 14.28571 4.428571  9.857143 3.428571 16.85714
A.K.




On Tuesday, March 18, 2014 6:47 PM, Jim Lemon <jim at bitwrit.com.au> wrote:
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


______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list