Quantcast

beginner's loop issue

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

beginner's loop issue

aledanda
Dear All,

I hope you don't mind helping me with this small issue. I haven't been using R in years and I'm trying to fill in a matrix
with the output of a function (I'm probably using the Matlab logic here and it's not working).
Here is my code:

for (i in 1:length(input)){
  out[i,1:3] <- MyFunction(input[i,1],input[i,2], input[i,3])
    out[i,4:6] <- MyFunction(input[i,5],input[i,7], input[i,6])
      out[i,7:9] <- MyFunction(input[i,8],input[i,10], input[i,9])
}

'input' is a matrix
> dim(input)
[1] 46 10

and each raw corresponds to a different subject.
The error I get here is

Error in out[i, 1:3] <- get.vaTer(input[i, 2], input[i, 4], input[i, 3],  :
  object 'out' not found


So I wonder, what's wrong in the assignment to the variable out?
Should I define the variable before the loop?

Thanks for your help
Best

Ale
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: beginner's loop issue

Michael Weylandt
Yes, the short answer is that you need to define out before running
the loop. The most effective way to do so will be to set up a matrix
with the exact right dimensions (if you know them up front); something
like out <- matrix(NA, nrow = length(input), ncol = 9)

Michael



On Tue, Mar 13, 2012 at 12:27 PM, aledanda <[hidden email]> wrote:

> Dear All,
>
> I hope you don't mind helping me with this small issue. I haven't been using
> R in years and I'm trying to fill in a matrix
> with the output of a function (I'm probably using the Matlab logic here and
> it's not working).
> Here is my code:
>
> for (i in 1:length(input)){
>  out[i,1:3] <- MyFunction(input[i,1],input[i,2], input[i,3])
>    out[i,4:6] <- MyFunction(input[i,5],input[i,7], input[i,6])
>      out[i,7:9] <- MyFunction(input[i,8],input[i,10], input[i,9])
> }
>
> 'input' is a matrix
>> dim(input)
> [1] 46 10
>
> and each raw corresponds to a different subject.
> The error I get here is
>
> /Error in out[i, 1:3] <- get.vaTer(input[i, 2], input[i, 4], input[i, 3],  :
>  object 'out' not found/
>
> So I wonder, what's wrong in the assignment to the variable out?
> Should I define the variable before the loop?
>
> Thanks for your help
> Best
>
> Ale
>
> --
> View this message in context: http://r.789695.n4.nabble.com/beginner-s-loop-issue-tp4469514p4469514.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: beginner's loop issue

PaulJohnson32gmail
In reply to this post by aledanda
On Tue, Mar 13, 2012 at 11:27 AM, aledanda <[hidden email]> wrote:

> Dear All,
>
> I hope you don't mind helping me with this small issue. I haven't been using
> R in years and I'm trying to fill in a matrix
> with the output of a function (I'm probably using the Matlab logic here and
> it's not working).
> Here is my code:
>
> for (i in 1:length(input)){
>  out[i,1:3] <- MyFunction(input[i,1],input[i,2], input[i,3])
>    out[i,4:6] <- MyFunction(input[i,5],input[i,7], input[i,6])
>      out[i,7:9] <- MyFunction(input[i,8],input[i,10], input[i,9])
> }
>
> 'input' is a matrix
>> dim(input)
> [1] 46 10
>
> and each raw corresponds to a different subject.
> The error I get here is
>
> /Error in out[i, 1:3] <- get.vaTer(input[i, 2], input[i, 4], input[i, 3],  :
>  object 'out' not found/

out has to exist first, as previous commenter said.

Furthermore, suggestions:

Consider making MyFunction accept a vector of 3 arguments, rather than
separate arguments.

Consider making out 3 columns, as in

out <- matrix(0, nrow=N, ncol=3)
for(i ...){
out[i,1:3] <- MyFunction(input[i,1:3])
out[i,1:3] <- MyFunction(input[i,4:6])
out[i,1:3] <- MyFunction(input[i,7:9])
}

If you could re-shape your input "thing" as a list with one element
that needs to go into MyFunction, this could get easier still:

lapply(input, MyFunction)

 or if input were an array with 3 columns, you could revise MyFuntion
to accept a 3-vector.

apply(input, 1, MyFunction)

Hardly ever in R does one need to specify inputs as you have done in
your example.
pj



--
Paul E. Johnson
Professor, Political Science    Assoc. Director
1541 Lilac Lane, Room 504     Center for Research Methods
University of Kansas               University of Kansas
http://pj.freefaculty.org            http://quant.ku.edu

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: beginner's loop issue

aledanda
Thank you all for your helpful comments!
I solved my problem by creating an empty matrix before the loop and adjusting the loop itself, this is the code:

size <- dim(input)
out <- matrix('',nrow =size[1], ncol =  9)

for (i in 1:nrow(input)) {
 
  out[i,1:3] <- unlist(MyFunction(input[i,1],input[i,2], input[i,3])
    out[i,4:6] <- unlist(MyFunction(input[i,5],input[i,7], input[i,6])
      out[i,7:9] <- unlist(MyFunction(input[i,8],input[i,10], input[i,9])

}

It's probably not the most efficient way of doing it but it gives me the desired output matrix...
Thanks for pointing me to this pdf R inferno pdf, I had an old version of it..
I'm, starting to use R again after long time and this is a very useful guide!

All the best

Ale


Loading...