[R] Efficient way to fill a matrix

Bert Gunter gunter.berton at gene.com
Thu Nov 6 00:16:30 CET 2008


I don't think that this answers the poster's question, as he explicitly
mentioned that some row and column indices could be missing.

I think it's simpler and much faster to do this by matrix indexing:

mx <- matrix(NA,nr = max(foo[,1]),ncol = max(foo[,2])) ## fill with NA's
mx[as.matrix(foo[,1:2])] <- foo[,3]

See ?"[" for details.

-- Bert Gunter

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of stephen sefick
Sent: Wednesday, November 05, 2008 2:52 PM
To: Philipp Pagel
Cc: r-help at r-project.org
Subject: Re: [R] Efficient way to fill a matrix

#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.



More information about the R-help mailing list