[R] Help wit matrices

Ravi Varadhan rvaradhan at jhmi.edu
Fri Aug 10 20:26:32 CEST 2007


An even simpler solution is:

mat2 <- 1 * (mat1 > 0.25)

Ravi.

----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Lanre Okusanya
Sent: Friday, August 10, 2007 2:20 PM
To: jim holtman
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Help wit matrices

that was ridiculously simple. duh.

THanks

Lanre

On 8/10/07, jim holtman <jholtman at gmail.com> wrote:
> Is this what you want:
>
> > x <- matrix(runif(100), 10)
> > round(x, 3)
>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
>  [1,] 0.268 0.961 0.262 0.347 0.306 0.762 0.524 0.062 0.028 0.226
>  [2,] 0.219 0.100 0.165 0.131 0.578 0.933 0.317 0.109 0.527 0.131
>  [3,] 0.517 0.763 0.322 0.374 0.910 0.471 0.278 0.382 0.880 0.982
>  [4,] 0.269 0.948 0.510 0.631 0.143 0.604 0.788 0.169 0.373 0.327
>  [5,] 0.181 0.819 0.924 0.390 0.415 0.485 0.702 0.299 0.048 0.507
>  [6,] 0.519 0.308 0.511 0.690 0.211 0.109 0.165 0.192 0.139 0.681
>  [7,] 0.563 0.650 0.258 0.689 0.429 0.248 0.064 0.257 0.321 0.099
>  [8,] 0.129 0.953 0.046 0.555 0.133 0.499 0.755 0.181 0.155 0.119
>  [9,] 0.256 0.954 0.418 0.430 0.460 0.373 0.620 0.477 0.132 0.050
> [10,] 0.718 0.340 0.854 0.453 0.943 0.935 0.170 0.771 0.221 0.929
> > ifelse(x > .5, 1, 0)
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>  [1,]    0    1    0    0    0    1    1    0    0     0
>  [2,]    0    0    0    0    1    1    0    0    1     0
>  [3,]    1    1    0    0    1    0    0    0    1     1
>  [4,]    0    1    1    1    0    1    1    0    0     0
>  [5,]    0    1    1    0    0    0    1    0    0     1
>  [6,]    1    0    1    1    0    0    0    0    0     1
>  [7,]    1    1    0    1    0    0    0    0    0     0
>  [8,]    0    1    0    1    0    0    1    0    0     0
>  [9,]    0    1    0    0    0    0    1    0    0     0
> [10,]    1    0    1    0    1    1    0    1    0     1
>
>
> On 8/10/07, Lanre Okusanya <lanre.okusanya at gmail.com> wrote:
> > Hello all,
> >
> > I am working with a 1000x1000 matrix, and I would like to return a
> > 1000x1000 matrix that tells me which value in the matrix is greater
> > than a theshold value (1 or 0 indicator).
> > i have tried
> >  mat2<-as.matrix(as.numeric(mat1>0.25))
> > but that returns a 1:100000 matrix.
> > I have also tried for loops, but they are grossly inefficient.
> >
> > THanks for all your help in advance.
> >
> > Lanre
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> >
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>

______________________________________________
R-help at stat.math.ethz.ch 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