nested forloop for nxn pixels

I have a current code for 3x3 pixels. I make my designed data format manually using the code below. I would like to loop it in such a way, for example user-input is 3x3 .. the code load the data starting from Row == 0 & Column == 0 and runs unitll Row == n-1 & Column == n-1 and makes the data matrix at the end.

I couldn't loop for nxn pixel

current file is 3 x 3 pixel

dat <- read.table("data.txt", header = T)

s1 <- dat[with(dat, Row == 0 & Column == 0), ] ## s1 means spectrum 0,0
s1o3 <- abs(s1$O3P)
r <- s1$Wavenumber
s1o3 <-new("hyperSpec",spc=s1o3,wavelength=r)
s1o3$x <- 0 #row 0
s1o3$y <- 0 #column 0
plot(s1o3[,,1500~1800])

s2 <- dat[with(dat, Row == 0 & Column == 1), ] ## s2 means spectrum 0,1
s2o3 <- abs(s2$O3P)
s2o3 <-new("hyperSpec",spc=s2o3,wavelength=r)
s2o3$x <- 0#row 0
s2o3$y <- 1#column 1
plot(s2o3[,,1500~1800])

s3 <- dat[with(dat, Row == 0 & Column == 2), ] ## s3 means spectrum 0,2....
s3o3 <- abs(s3$O3P)
s3o3 <-new("hyperSpec",spc=s3o3,wavelength=r)
s3o3$x <- 0#row 0
s3o3$y <- 2#column 2
plot(s3o3[,,1500~1800])

s4 <- dat[with(dat, Row == 1 & Column == 0), ] ## s4 means spectrum 1,0 (next line)
s4o3 <- abs(s4$O3P)
s4o3 <-new("hyperSpec",spc=s4o3,wavelength=r)
s4o3$x <- 1#row 1
s4o3$y <- 0#column 0
plot(s4o3[,,1300~1800])

s5 <- dat[with(dat, Row == 1 & Column == 1), ]
s5o3 <- abs(s5$O3P)
s5o3 <-new("hyperSpec",spc=s5o3,wavelength=r)
s5o3$x <- 1#row 1
s5o3$y <- 1#column 1
plot(s5o3[,,1300~1800])

s6 <- dat[with(dat, Row == 1 & Column == 2), ]
s6o3 <- abs(s6$O3P)
s6o3 <-new("hyperSpec",spc=s6o3,wavelength=r)
s6o3$x <- 1#row 1
s6o3$y <- 2#column 2
plot(s6o3[,,1300~1800])

s7 <- dat[with(dat, Row == 2 & Column == 0), ]
s7o3 <- abs(s7$O3P)
s7o3 <-new("hyperSpec",spc=s7o3,wavelength=r)
s7o3$x <- 2#row 2
s7o3$y <- 0#column 0
plot(s7o3[,,1300~1800])

s8 <- dat[with(dat, Row == 2 & Column == 1), ]
s8o3 <- abs(s8$O3P)
s8o3 <-new("hyperSpec",spc=s8o3,wavelength=r)
s8o3$x <- 2#row 2
s8o3$y <- 1#column 1
plot(s8o3[,,1300~1800])

s9 <- dat[with(dat, Row == 2 & Column == 2), ]
s9o3 <- abs(s9$O3P)
s9o3 <-new("hyperSpec",spc=s9o3,wavelength=r)
s9o3$x <- 2 #row 2
s9o3$y <- 2#column 2
plot(s9o3[,,1300~1800])

data <- rbind(s1o3,s2o3,s3o3,s4o3,s5o3,s6o3,s7o3,s8o3,s9o3)

thanks for the help in advance

Are you trying to make a for loop?
Here is an example of a for loop in R.

for (x in 1:10) {
  print(x)
}
1 Like

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.