ingaschwabe
2012-Oct-18 10:35 UTC
[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.
Pieter Schoonees
2012-Oct-18 12:20 UTC
[R] For loop with i and j and multiple if statements... help!
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-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of ingaschwabe > Sent: Thursday 18 October 2012 12:36 > To: r-help at 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 at 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.
Sarah Goslee
2012-Oct-18 12:34 UTC
[R] For loop with i and j and multiple if statements... help!
If you use ifelse() you don't need the loops at all since ifelse() is vectorized. But I don't have any idea why you need two loops: do you really need to compare all pairs of observations? Also, the logic in your if statements is not the same as the logic in your problem description. Sarah On Thursday, October 18, 2012, ingaschwabe wrote:> 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 <javascript:;> 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. >-- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]]
Jessica Streicher
2012-Oct-18 12:46 UTC
[R] For loop with i and j and multiple if statements... help!
- Why are you making a matrix for lookzone if it is ever only one number? - changing a variable used for iteration is bad practice (input in this case) though you aren't changing the rows i guess. - There are too many x and too few y in those ifs. Plus this is shorter: test<-apply(input,1,function(x){ lookzone<-NULL if (x[1] > 170 && x[1] < 1250 && x[2] > 150 && x[2] < 480) {lookzone<-1} if (x[1] > 170 && x[1] < 420 && x[2] > 480 && x[2] < 810) {lookzone <- 2} return(lookzone) }) And should get you a list with the look zones. On 18.10.2012, at 12:35, ingaschwabe wrote:> 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 at 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.