[R] For loop with i and j and multiple if statements... help!
Jessica Streicher
j.streicher at micromata.de
Thu Oct 18 14:46:45 CEST 2012
- 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.
More information about the R-help
mailing list