Reputation: 699
v=c(96,94,101,84,99,106,85,98,98,85,95,108,84,98,114,86,97,109,84,95,105,83,97,
100,81,93,102)
rep1=array(v,dim=c(3,3,3))
u=c(84,95,105,85,97,104,86,90,103,80,93,110,82,99,102,84,95,100,83,92,102,80,96,
111,79,93,108)
rep2=array(u,dim=c(3,3,3))
rep=rep1+rep2
f=function(x1,x2,x3){
l=x1
return(l)
}
for(i in 1:3){
for(j in 1:3){
for(k in 1:3){
if(f(i,j,k)%%3==1)L=rep[i,j,k]
}
}
}
here L is taking the last value for which the condition is satisfied . But i want that L will be a vector which will take all the values that will satisfy the condition.
Upvotes: 1
Views: 40
Reputation: 3429
Here is a solution based on your loop:
L <- c()
for(i in 1:3){
for(j in 1:3){
for(k in 1:3){
if(f(i,j,k)%%3==1) L <- c(L, rep[i,j,k])
}
}
}
# [1] 180 165 167 169 166 163 171 170 160
Here is another more esoteric one:
as.vector( t( rep[ (1:3) %% 3 == 1,,] ) )
# [1] 180 165 167 169 166 163 171 170 160
Upvotes: 3