Hi,

I would like to have an index for a column in a matrix encoded in a
cell of the same matrix.
For example:
x = matrix(c(11,12,13,1,
21,22,23,3,
31,32,33,2),byrow=T,ncol=4)

In this case, column 4 is the index. I then access the column
specified in the index by:
> for (i in 1:3) print(x[i,x[i,4]])
[1] 11
[1] 23
[1] 32
>
> for (i in 1:3) {x[i,x[i,4]] <- x[i,x[i,4]] + 5}
> x
     [,1] [,2] [,3] [,4]
[1,]   16   12   13    1
[2,]   21   22   28    3
[3,]   31   37   33    2
>

Is there a way to get the same results without looping?

Best,

Iuri.

______________________________________________
R-help@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.

Reply via email to