Matrix in R

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix in R

danielepippo
Hi everyone,
   I'm building a matrix in R with a cycle for like this:
pp_ris2=matrix(NA,6,6)
for(i in 0:6){
        for(j in 0:6){
                if(i>j){
                pp_ris2[i,j]=myfunction}
        else if(i==j){
                print(c(i,j))
                pp_ris2[i,j]=myfunction}
       
}}


but the result is:
      [,1]     [,2]     [,3]    [,4]   [,5]    [,6]
[1,] 0.062   NaN    NaN    NaN   NaN   NaN
[2,] 0.083  0.022   NaN    NaN   NaN   NaN
[3,] 0.072  0.022  0.004   NaN   NaN   NaN
[4,] 0.046  0.016  0.003  0.001  NaN   NaN
[5,] 0.023  0.009  0.002  0.000   0      NaN
[6,] 0.010  0.004  0.001  0.000   0        0

my problem is in the first column and row because in this matrix there are not the column and the row with i=0 and j=0. Can anyone help me please?

thanks a lot

Reply | Threaded
Open this post in threaded view
|

Re: Matrix in R

Dieter Menne
danielepippo wrote
   I'm building a matrix in R with a cycle for like this:
pp_ris2=matrix(NA,6,6)
for(i in 0:6){
.....
R is not like c, indexing starts with 1.

Dieter

Reply | Threaded
Open this post in threaded view
|

Re: Matrix in R

danielepippo
but if in my function
pp_ris2[i,j]=myfunction}
must be the indexes 0-0,0-1,0-2,0-3, ....?
Reply | Threaded
Open this post in threaded view
|

Re: Matrix in R

David Winsemius

On Feb 18, 2011, at 9:32 AM, danielepippo wrote:

>
> but if in my function
> pp_ris2[i,j]=myfunction}
> must be the indexes 0-0,0-1,0-2,0-3, ....?
>

 From a search of RSiteSearch() that started with with terms:  zero  
matrix indexing

http://finzi.psych.upenn.edu/R/Rhelp02/archive/39031.html
http://finzi.psych.upenn.edu/R/Rhelp02/archive/26476.html
http://finzi.psych.upenn.edu/R/Rhelp02/archive/32960.html


> --
> View this message in context: http://r.789695.n4.nabble.com/Matrix-in-R-tp3312748p3312780.html
> Sent from the R help mailing list archive at Nabble.com.


--
David Winsemius, MD
West Hartford, CT

______________________________________________
[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
|

Re: Matrix in R

Philipp Pagel-5
In reply to this post by danielepippo
On Fri, Feb 18, 2011 at 06:32:01AM -0800, danielepippo wrote:
>
> but if in my function
> pp_ris2[i,j]=myfunction}
> must be the indexes 0-0,0-1,0-2,0-3, ....?

You'll have to take care of that yourself with a bit of index
arithmetics. It's  the same you encounter in C, if you are
modelling something that would like to be indexed starting with 1 -
just the other way round.

cu
        Philipp

--
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
Maximus-von-Imhof-Forum 3
85354 Freising, Germany
http://webclu.bio.wzw.tum.de/~pagel/

______________________________________________
[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
|

Re: Matrix in R

David Winsemius
In reply to this post by danielepippo

On Feb 18, 2011, at 9:32 AM, danielepippo wrote:

>
> but if in my function
> pp_ris2[i,j]=myfunction}
> must be the indexes 0-0,0-1,0-2,0-3, ....?

I came across a posting in r-help that called this package "blasphemy":

http://cran.r-project.org/web/packages/Oarray/index.html
>

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[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.