[R] Accessing values of a matrix

Sarah Goslee sarah.goslee at gmail.com
Fri Feb 19 19:07:28 CET 2010


Hi Colin,

You'll get a better answer if you provide an actual example, including what
you tried that didn't work. It looks to me like you want to subset a
data frame based
on column names, though I'm not sure what A(i, J) is supposed to be.

if so, then this is one approach:

> A <- data.frame(A = 1:3, B = 4:6, C=7:9)
> A
  A B C
1 1 4 7
2 2 5 8
3 3 6 9
> J <- c("A", "C")
> newA <- A[, colnames(A) %in% J]
> newA
  A C
1 1 7
2 2 8
3 3 9

Sarah

On Fri, Feb 19, 2010 at 12:11 PM, statquant <colin.umansky at barcap.com> wrote:
>
> hello all,
> thank you for taking the time
>
> I have a matrix A that have column names (let say n columns), I want to
> reduce the matrix to have just a few of those column (p colums, this is
> trivial), but for the lines I want only the lines such that A(i,J) is part
> of a list (J is fixed and known)
>
> I am sure it is very easy but I don't find it (I tryed which but it doesn't
> seem to work)
> Surely I could do a loop but I want to learn how to do things without time
> consuming loops.
>
> Thanks
> Colin
> --
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list