Re: [R] Efficient way to fill a matrix

From: stephen sefick <ssefick_at_gmail.com>
Date: Wed, 05 Nov 2008 17:52:19 -0500

#reshape package should do it
library(reshape)
foo <- data.frame(row=1:5, col=1:3, val=rnorm(15)) cast(foo, row~col)

On Wed, Nov 5, 2008 at 5:47 PM, Philipp Pagel <p.pagel_at_wzw.tum.de> wrote:
>
> Dear R experts,
>
> Suppose I have a data frame of three variables:
>
>> foo <- data.frame(row=1:5, col=1:3, val=rnorm(15))
>> foo
> row col val
> 1 1 1 -1.00631642
> 2 2 2 0.77715344
> 3 3 3 0.17358793
> 4 4 1 -1.67226988
> 5 5 2 1.08218836
> 6 1 3 1.32961329
> 7 2 1 -0.51186267
> 8 3 2 -1.20990127
> 9 4 3 -0.57786899
> 10 5 1 0.67102887
> 11 1 2 0.05646411
> 12 2 3 0.01146612
> 13 3 1 -3.12094409
> 14 4 2 -1.01932191
> 15 5 3 0.76736702
>
>
> I want to turn this into a matrix of val according to row and col. Let's also
> assume that some combinations of row and col are missing - i.e. there will be
> NAs in the resulting Matrix. My current approach is simple and works but is
> slow for large datasets:
>
> mat <- matrix(nrow=max(foo$row), ncol=max(foo$col))
> for (line in 1:dim(foo)[1]) {
> mat[foo[line, 'row'], foo[line, 'col']] <- foo[line, 'val']
> }
>
>> mat
> [,1] [,2] [,3]
> [1,] -1.0063164 0.05646411 1.32961329
> [2,] -0.5118627 0.77715344 0.01146612
> [3,] -3.1209441 -1.20990127 0.17358793
> [4,] -1.6722699 -1.01932191 -0.57786899
> [5,] 0.6710289 1.08218836 0.76736702
>
>
> Can anyone think of a more efficient way?

>
> cu
> Philipp
>
> --
> Dr. Philipp Pagel
> Lehrstuhl für Genomorientierte Bioinformatik
> Technische Universität München
> Wissenschaftszentrum Weihenstephan
> 85350 Freising, Germany
> http://mips.gsf.de/staff/pagel
>
> ______________________________________________
> 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.
>

-- 
Stephen Sefick
Research Scientist
Southeastern Natural Sciences Academy

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

								-K. Mullis
______________________________________________
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.
Received on Wed 05 Nov 2008 - 22:56:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Nov 2008 - 00:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive