[R] Indexing problem with matrix

Bogaso Christofer bogaso.christofer at gmail.com
Fri Jul 22 19:18:32 CEST 2011


Thanks Joshua and Berton for your pointers. Yes it was typo as I wanted to mean to select few columns.

-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com] 
Sent: 22 July 2011 22:02
To: Bogaso Christofer
Cc: r-help at r-project.org
Subject: Re: [R] Indexing problem with matrix

On Fri, Jul 22, 2011 at 9:46 AM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Dear all, assume I have a matrix with just 1 row. Now suppose I want to
> fetch 1st few rows from that matrix, however resulting object becomes

I'm assuming you mean the first few columns.  The place to look is
?"[" where you would find the drop argument.

matrix(1:5, 1)[, -1, drop = FALSE]

Cheers,

Josh

> vector. Here is 1 such example:
>
>
>
>> matrix(1:5, 1)
>
>     [,1] [,2] [,3] [,4] [,5]
>
> [1,]    1    2    3    4    5
>
>>
>
>> matrix(1:5, 1)[,-1]
>
> [1] 2 3 4 5
>
>
>
> Can somebody point me how to keep resulting object as matrix with same row?
> Ofcourse I again make legitimate matrix with something like as.matrix()
> function. However I believe there must be some more directly way with
> indexing in some better way!
>
>
>
> Thanks,
>
>
>        [[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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list