[R] Subcripting matrix

Berend Hasselman bhh at xs4all.nl
Fri Sep 20 13:12:43 CEST 2013


On 20-09-2013, at 12:52, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:

> Hello again,
> 
> I have one question on subscripting matrix. Let say I have following matrix:
> 
>> Mat <- matrix(1:9, 3)
>> colnames(Mat) <- c("a", "b", "a")
>> Mat
>     a b a
> [1,] 1 4 7
> [2,] 2 5 8
> [3,] 3 6 9
> 
> 
> Now I want to fetch data for colnames 'a'.I did following:
> 
>> Mat[, "a"]
> [1] 1 2 3
> 
> 
> However it is not taking second 'a' colume. Basically I expected to
> get 1st and 3rd columns
> 
> Can somebody tell me how to achieve that?
> 

I think this is just silly.

How about

Mat[,which(colnames(Mat)=="a")]

Berend



More information about the R-help mailing list