Without commenting on the method itself, shouldn't you be using both x and y twice within each loop instead of x three times and y only once?
Please provide a snippet of your data by including the output of dput(head(data,30)) for example. > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of ingaschwabe > Sent: Thursday 18 October 2012 12:36 > To: r-help@r-project.org > Subject: [R] For loop with i and j and multiple if statements... help! > > Dear all! > > I'm quite new to R and at this moment a little bit lost in trying to > make a function work with a for loop that has two variables (i and j) > and multiple if statements.. > I hope that someone of you could help me. I would very appreciate it! > > What do I want to do? I am analysing the data of an 'eye tracker'. The > eye tracker registers the coordinates to which participants looked > while reading a website. > > The data looks like this: > > Number x y > 1 1 701 209 > 2 2 685 209 > 3 3 566 248 > 4 4 562 234 > 5 5 601 225 > 6 6 608 232 > [....] > > To assign look zones, I looked which zone (e.g. the references) is > represented by which coordinates. This give the following results: > Look zone 1 = x > 170, x < 1250, y > 150 and y < 480 look sone 2 = x > > 170, x < 420, y > 480 and y < 810. > > Now I would like to assign a lookzone for each subject by the means of > a function with a for loop. > > I've made this: > > lookzones <- function(input){ > > lookzone <- matrix(0, nrow(input), 1)#Make space for > lookzone variables > > for(i in 1:nrow(input)){ > for(j in 1:nrow(input)){ > if (x[i] > 170 && x[i] < 1250 && x[j] > 150 && y[j] < > 480) {lookzone <- 1} > if (x[i] > 170 && x[i] < 420 && x[j] > 480 && y[j] < > 810) {lookzone <- 2} > } > list(lookzone = lookzone) > input <- cbind(input, lookzone) > } > } > > First, there is a variable made (lookzone) in which the values of the > lookzones can be stored. Then in the for loop, it is tested whether all > conditions are met (see above) and then a value is assigned to the > lookzone variable. > The input variable is a data set that looks like the data above. > > However, the function does not seem to work. > When I run it, R assigns a value '1' to every x/y combination. > > Can someone help me with this? > > Thank you so much ! > > Bye, inga > > > > -- > View this message in context: http://r.789695.n4.nabble.com/For-loop- > with-i-and-j-and-multiple-if-statements-help-tp4646596.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.