[HELP!] with assigning four adjacent points in an array

Hi,

I am relatively a beginner on r studio and have been assigned a task with arrays.
I have a 20x20 array on r. I will need to be able to locate 4 points thats next to the point we're focused on i.e centre. (So the points next to the centre will be the one ,above, left,right and bottom)
This is quite easy for me to do and I have a written a code for this.

The tricky part is the borders. where i and j is rows and columns .

If i and j is both 1 S[1,1] , i will need the code to recognize that when the array coordinate is S[1,1]
the point above and left will be S[20,1] and S[1,20] for this point. This has to be done for all sides and borders

can anyone help?? the code below is what i have so far

Q <- sample(c(1), 400, replace = T)

S <- array(Q, dim = c(n, n))

x<-3
y<-14

centre<-S[x,y] #iNput a point within the array
fcentre<- -1*(centre) #Flipped centre

r<-S[x+1,y]
l<-S[x-1,y]
u<-S[x,y+1]
d<-S[x,y-1]

a<-S[1,n]
b<-S[n,1]
print(a)

if (x == 1 & y == 1){
Ebefore1=(centre)(r+a+b+d)
Eafter1<-(fcentre)
(r+a+b+d)
de=Ebefore1-Eafter1

print(Ebefore1)
print(de)

}

if (x>1 & y>1 & x<n & y<n){

Ebefore<-(centre)(u+d+l+r)
Eafter<-(fcentre)
(u+d+l+r)
de=Ebefore-Eafter
print(de)
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.