Re: [R] writing a function

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Fri, 8 Feb 2008 13:13:55 -0500 (EST)


It is doing exactly what you ask. You are asking for the last element in the matrix w[i,j] and the last element in the vector y[i].

Try return(w) and return(y).

> Dear lists,
>
> I'm in my process of learning of writing a
> function. I tried to write a simple functions of a
> matrix and a vector. Here are the codes:
>
> mm<-function(m,n){ #matrix function
>
> w<-matrix(nrow=m, ncol=n)
> for(i in 1:m){
> for(j in 1:n){
> w[i,j]=i+j
> }
> }
> return(w[i,j])
> }
>
> v<-function(n){ #function of a vector
> y=vector(length=n)
> for(i in 1:n){
> y[i]=i
> }
> return(y[i])
> }
>
> when i tried called on my matrix function; say
> mm(5,10). what i got is the single value of
> 15..where's is my matrix?? same thing happened to my
> vector function. Please help me figure out these
> problems. Thanks a bunch!!
>
> Cheers,
> Anisah
>
>
>
>
>
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

      Looking for the perfect gift? Give the gift of Flickr!



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. Received on Fri 08 Feb 2008 - 18:17:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Feb 2008 - 20:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive