manipulating a matrix

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

manipulating a matrix

Clark Johnston
Is there a way to create a new matrix from and existing matrix with

A
          [,1]     [,2]
[1,]      1        13
[2.]      2        18
[3,]      3        14
[4,]      4        20

if(A[,2] > 15)
   
B
       [,1]   [,2]    
[1,]   2      18
[2,]   4      20

Reply | Threaded
Open this post in threaded view
|

Re: manipulating a matrix

Henrique Dallazuanna
Try:

A[A[,2] > 15,]

On Wed, May 12, 2010 at 4:14 PM, Clark Johnston <[hidden email]>wrote:

>
> Is there a way to create a new matrix from and existing matrix with
>
> A
>          [,1]     [,2]
> [1,]      1        13
> [2.]      2        18
> [3,]      3        14
> [4,]      4        20
>
> if(A[,2] > 15)
>
> B
>       [,1]   [,2]
> [1,]   2      18
> [2,]   4      20
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/manipulating-a-matrix-tp2196610p2196610.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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[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: manipulating a matrix

Erik Iverson-3
In reply to this post by Clark Johnston


Clark Johnston wrote:

> Is there a way to create a new matrix from and existing matrix with
>
> A
>           [,1]     [,2]
> [1,]      1        13
> [2.]      2        18
> [3,]      3        14
> [4,]      4        20
>
> if(A[,2] > 15)
>    
> B
>        [,1]   [,2]    
> [1,]   2      18
> [2,]   4      20
>
>

It's easiest to get help if you give your objects as the output from ?dput.

This is just simple indexing:

B <- A[A[, 2] > 15, ]

______________________________________________
[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: manipulating a matrix

David Winsemius
In reply to this post by Clark Johnston

On May 12, 2010, at 3:14 PM, Clark Johnston wrote:

>
> Is there a way to create a new matrix from and existing matrix with
>
> A
>          [,1]     [,2]
> [1,]      1        13
> [2.]      2        18
> [3,]      3        14
> [4,]      4        20
>
> if()
>
> B
>       [,1]   [,2]
> [1,]   2      18
> [2,]   4      20

Try (untested absent reproducible example);

A[A[,2] > 15, ]

This (should) create a logical vector in the first argument to "["  
which has the effect of returning the rows for which that vector is  
TRUE.

>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/manipulating-a-matrix-tp2196610p2196610.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.

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.