Re: [R] For loop with i and j and multiple if statements... help!

2012-10-19 Thread ingaschwabe
Again, thank you all for the replies (and for the free R lesson!) 

You helped me a lot and I very appreciate it. 

I will work my self trough the for and apply section of my R manual again.. 

Thanks,

Bye, inga





--
View this message in context: 
http://r.789695.n4.nabble.com/For-loop-with-i-and-j-and-multiple-if-statements-help-tp4646596p4646751.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.


Re: [R] For loop with i and j and multiple if statements... help!

2012-10-18 Thread Pieter Schoonees
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.


Re: [R] For loop with i and j and multiple if statements... help!

2012-10-18 Thread Sarah Goslee
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]]

__
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.


Re: [R] For loop with i and j and multiple if statements... help!

2012-10-18 Thread Jessica Streicher
- 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@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.


Re: [R] For loop with i and j and multiple if statements... help!

2012-10-18 Thread ingaschwabe
Dear all, 

Thanks for the many replies!

Indeed, the function does not represent my explanation. It should have been: 

if (x[i]  170  x[i]  1250  y[j]  150  y[j]  480) 

Sorry, my mistake. 

My data looks like this: 
head(input[,3:4])
x   y
1 701 209
2 685 209
3 566 248
4 562 234
5 601 225
6 608 232


Refering to the last post, I guess that my function then should look like
something like this: 

test - apply(input,1,function(x){ 
  lookzone-NULL 
  if (x[1]  170  x[1]  1250  y[1]  150  y[1]  480)
  {lookzone-1} 
  if (x[1]  170  x[1]  420   y[1]  480  y[1]  810)
  {lookzone - 2} 
  return(lookzone)
  })

however, when I run the function then nothing happens. 

and when I ask explicitly for the result then I get this 
 test
NULL

Can someone tell me what goes wrong here? Or give a hint? 
I'm not used to using the apply function and do use the for loop on the
basis of an example that I once made so I am not a very experienced R user.. 

Thank you so much for your help!
I really appreciate it!

Bye,inga



--
View this message in context: 
http://r.789695.n4.nabble.com/For-loop-with-i-and-j-and-multiple-if-statements-help-tp4646596p4646630.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.


Re: [R] For loop with i and j and multiple if statements... help!

2012-10-18 Thread Sarah Goslee
There are a bunch of things wrong with your code, from not
understanding subsetting to only returning a single value rather than
a vector of values. I highly recommend that you read the Introduction
to R document that came with your R installation and is readily
available online.

In the meantime:

testdata - structure(list(x = c(701L, 685L, 566L, 562L, 601L, 608L),
y = c(209L,
209L, 248L, 234L, 225L, 232L)), .Names = c(x, y), class =
data.frame, row.names = c(1,
2, 3, 4, 5, 6))
lookzone - with(testdata, ifelse(x  170  x  1250  y  150  y 
480, 1, ifelse(x  170  x  420  y  480  y  810, 2, NA)))
 lookzone
[1] 1 1 1 1 1 1

Or, with sample data that includes all the combinations:
 testdata - data.frame(x = c(701, 685, 100, 100, 1300, 400, 400), y=c(209, 
 209, 100, 300, 300, 500, 100)) lookzone - with(testdata, ifelse(x  170  x 
  1250  y  150  y  480, 1, ifelse(x  170  x  420  y  480  y  810, 
 2, NA)))
 lookzone
[1]  1  1 NA NA NA  2 NA

Sarah


On Thu, Oct 18, 2012 at 10:51 AM, ingaschwabe ingaschw...@gmail.com wrote:
 Dear all,

 Thanks for the many replies!

 Indeed, the function does not represent my explanation. It should have been:

 if (x[i]  170  x[i]  1250  y[j]  150  y[j]  480)

 Sorry, my mistake.

 My data looks like this:
 head(input[,3:4])
 x   y
 1 701 209
 2 685 209
 3 566 248
 4 562 234
 5 601 225
 6 608 232


 Refering to the last post, I guess that my function then should look like
 something like this:

 test - apply(input,1,function(x){
   lookzone-NULL
   if (x[1]  170  x[1]  1250  y[1]  150  y[1]  480)
   {lookzone-1}
   if (x[1]  170  x[1]  420   y[1]  480  y[1]  810)
   {lookzone - 2}
   return(lookzone)
   })

 however, when I run the function then nothing happens.

 and when I ask explicitly for the result then I get this
 test
 NULL

 Can someone tell me what goes wrong here? Or give a hint?
 I'm not used to using the apply function and do use the for loop on the
 basis of an example that I once made so I am not a very experienced R user..

 Thank you so much for your help!
 I really appreciate it!

 Bye,inga





-- 
http://www.functionaldiversity.org

__
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.


Re: [R] For loop with i and j and multiple if statements... help!

2012-10-18 Thread Pieter Schoonees
Replace the y[1] with x[2] in the apply function - there is no 'y' defined in 
the function passed to apply. In the apply function, x is an 2-element vector 
representing a single row of the data (since the 1 in the second argument of 
apply says that it is applied row-wise). So the second element, i.e. x[2], is 
actually the y value. The name 'x' in function has nothing to do with the 
column 'x' in the data - it is the name of the argument sent to the function. 

Also, Sarah's code is vectorized (thus more elegant and efficient etc, and does 
not use a loop) so I suggest you stick with that.



 -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 4:51
 To: r-help@r-project.org
 Subject: Re: [R] For loop with i and j and multiple if statements...
 help!
 
 Dear all,
 
 Thanks for the many replies!
 
 Indeed, the function does not represent my explanation. It should have
 been:
 
 if (x[i]  170  x[i]  1250  y[j]  150  y[j]  480)
 
 Sorry, my mistake.
 
 My data looks like this:
 head(input[,3:4])
 x   y
 1 701 209
 2 685 209
 3 566 248
 4 562 234
 5 601 225
 6 608 232
 
 
 Refering to the last post, I guess that my function then should look
 like something like this:
 
 test - apply(input,1,function(x){
   lookzone-NULL
   if (x[1]  170  x[1]  1250  y[1]  150  y[1] 
 480)
   {lookzone-1}
   if (x[1]  170  x[1]  420   y[1]  480  y[1] 
 810)
   {lookzone - 2}
   return(lookzone)
   })
 
 however, when I run the function then nothing happens.
 
 and when I ask explicitly for the result then I get this
  test
 NULL
 
 Can someone tell me what goes wrong here? Or give a hint?
 I'm not used to using the apply function and do use the for loop on the
 basis of an example that I once made so I am not a very experienced R
 user..
 
 Thank you so much for your help!
 I really appreciate it!
 
 Bye,inga
 
 
 
 --
 View this message in context: http://r.789695.n4.nabble.com/For-loop-
 with-i-and-j-and-multiple-if-statements-help-tp4646596p4646630.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.