[R] folding table into a matrix
Spencer Graves
spencer.graves at pdf.com
Thu Sep 23 22:22:37 CEST 2004
Have you looked at "index arrays" in "An Introduction to R",
available as the first menu option after "help.start()" in R? The
version I got with R 1.9.1 for Windows includes the following:
> x <- array(1:20,dim=c(4,5)) # Generate a 4 by 5 array.
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> i <- array(c(1:3,3:1),dim=c(3,2))
> i # i is a 3 by 2 index array.
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
> x[i] # Extract those elements
[1] 9 6 3
> x[i] <- 0 # Replace those elements by zeros.
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 0 13 17
[2,] 2 0 10 14 18
[3,] 0 7 11 15 19
[4,] 4 8 12 16 20
>
The key point here is that "i" is a 3x2 array, and x[i] references
the 3 elements of x that have the row and column indices in x. Does
this provide the information you need?
hope this helps. spencer graves
Gene Cutler wrote:
> I'm just getting started with R, so feel free to point me to the
> appropriate documentation if this is already answered somewhere
> (though I've been unable to find it myself). This does seem like a
> rather basic question.
> I want to fold a table into a matrix. The table is formatted like so:
>
> Column_Index Value
> 1 486
> 2 688
> 3 447
> 4 555
> 5 639
> 1 950
> 2 881
> 3 1785
> 4 1216
> 1 612
> 2 790
> 3 542
> 4 1310
> 5 976
>
> And I want to end up with something like this:
>
> [,1] [,2] [,3] [,4] [,5]
> [1,] 486 688 447 555 639
> [2,] 950 881 1785 1216 NA
> [3,] 612 790 512 1310 976
>
> Since not all the rows are complete, I can't just reformat using
> matrix(), I need to go by the index information in the Column_Index
> column. This seems like something simple to do, but I'm stumped.
>
> Thanks.
>
> -- Gene
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
--
Spencer Graves, PhD, Senior Development Engineer
O: (408)938-4420; mobile: (408)655-4567
More information about the R-help
mailing list