-1

spouse i have a vector tmp of size 100 i want to know where there is for example an average of 10 between each 4 elements. i.e i want to know which of these: mean(tmp[c(1,2,3,4)]),mean(tmp[c(2,3,4,5)]),mean(tmp[c(3,4,5,6)])..and so on...mean(tmp[c(97,98,99,100)]) are larger then 10

how can i do it not in a loop? (loop takes too long since i have a table of 500000 rows by 60 col) and more not only avg but also difference or sum and so on... i have tried splitting rows as such

tmp<-seq(1,100,1)
one<-seq(1,97,1)
two<-seq(2,98,1)
tree<-seq(3,99,1)
four<-seq(4,100,1)
aa<-(tmp[one]+tmp[two]+tmp[tree]+tmp[four])/4
which(aa>10)

its working but its not rational to do it if you want for example avg of 12

here is an example of what i do to be clear

b12<-seq(1,988,1)
b11<-seq(2,989,1)
b10<-seq(3, 990,1)
b9<-seq(4,991,1)
b8<-seq(5,992,1)
b7<-seq(6,993,1)
b6<-seq(7,994,1)
b5<-seq(8, 995,1)
b4<-seq(9,996,1)
b3<-seq(10,997,1)
b2<-seq(11,998,1)
b1<-seq(12,999,1)
now<-seq(13, 1000,1)
po<-rpois(1000,4)
nor<-rnorm(1000,5,0.2)
uni<-runif(1000,10,75)
chis<-rchisq(1000,3,0)
which((po[now]/nor[now])>1  & (nor[b12]/nor[now])>1 & 
    ((po[now]/po[b4])>1 | (uni[now]-uni[b4])>=0) &
    ((chis[now]+chis[b1]+chis[b2]+chis[b3])/4)>2 &
    (uni[now]/max(uni[b1],uni[b2],uni[b3],uni[b4],
                    uni[b5],uni[b6],uni[b7],uni[b8]))>0.5)+12

this code give me the exact index in the real table that mach all the conditions

and i have 58 vars with 550000 rows

thank you

  • 1
    what exactly do you want/mean? you mean the average of 12 numbers instead of 4? – phonixor Jul 07 '14 at 13:09
  • or 10 instead of 12 (in which case i don't see the problem). – phonixor Jul 07 '14 at 13:15
  • i want to implement function like mean max diff... on a vector where i need to do it on every range for example i want to know what is the mean of the numbers in positions:1,2,3,4 and then the mean of the numbers in positions:2,3,4,5 and so on up to the mean in positions:97,98,99,100. then i need to know what is the position of the means that are above a in the original vector – user3812439 Jul 10 '14 at 13:22

2 Answers2

1

The question is not very clear. Based on the wording, I guess, this should help:

n <- 100

res <- sapply(1:(n-3), function(i) mean(tmp[i:(i+3)]))
which(res >10)

Also,

  m1 <- matrix(tmp[1:4+ rep(0:96,each=4)],ncol=4,byrow=T)
   which(rowMeans(m1) >10)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • ok thanks but it give me the position index of the output vector and i need to know where is it in the input one so in this case its no problem just add 3 but what if i need to check where the mean of 4 is >10 and the mean of 8 is <12 for example how will i find the index that it happens in the original vector? also how can i implement something like diff between value to the value of 4 rows before? – user3812439 Jul 10 '14 at 13:27
1

Maybe you should look at the rollapply function from the "zoo" package. You would need to adjust the width argument according to your specific needs.

library(zoo)
tmp <- seq(1, 100, 1)
rollapply(tmp, width = 4, FUN = mean)
#  [1]  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5 15.5
# [15] 16.5 17.5 18.5 19.5 20.5 21.5 22.5 23.5 24.5 25.5 26.5 27.5 28.5 29.5
# [29] 30.5 31.5 32.5 33.5 34.5 35.5 36.5 37.5 38.5 39.5 40.5 41.5 42.5 43.5
# [43] 44.5 45.5 46.5 47.5 48.5 49.5 50.5 51.5 52.5 53.5 54.5 55.5 56.5 57.5
# [57] 58.5 59.5 60.5 61.5 62.5 63.5 64.5 65.5 66.5 67.5 68.5 69.5 70.5 71.5
# [71] 72.5 73.5 74.5 75.5 76.5 77.5 78.5 79.5 80.5 81.5 82.5 83.5 84.5 85.5
# [85] 86.5 87.5 88.5 89.5 90.5 91.5 92.5 93.5 94.5 95.5 96.5 97.5 98.5

So, to get the details you want:

aa <- rollapply(tmp, width = 4, FUN = mean)
which(aa > 10)
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • ok thanks but it give me the position index of the output vector and i need to know where is it in the input one so in this case its no problem just add 3 but what if i need to check where the mean of 4 is >10 and the mean of 8 is <12 for example how will i find the index that it happens in the original vector? also how can i implement something like diff between value to the value of 4 rows before? – user3812439 Jul 10 '14 at 13:17
  • @user3812439, my answer replicates exactly what you showed as "working" code in your question. If you have a subsequent question, I'd suggest coming up with a working example (like you did here) and explaining what you hope to solve. Comments aren't exactly conducive to sharing such information if only because of space and formatting restrictions. – A5C1D2H2I1M1N2O1R2T1 Jul 10 '14 at 13:18