Sent: Tuesday, December 07, 2010 11:00 AM
> To: Greg Snow
> Cc: Gene; r-help@r-project.org
> Subject: Re: [R] Efficient way to use data frame of indices to
> initialize matrix
>
>
> On Dec 7, 2010, at 1:49 PM, Greg Snow wrote:
>
> > tmpdf <- data.frame( x = c(
On Dec 7, 2010, at 1:49 PM, Greg Snow wrote:
tmpdf <- data.frame( x = c(1,2,3), y=c(2,3,1), a=c(10,20,30) )
mymat <- matrix(0, ncol=3, nrow=3)
mymat[ as.matrix(tmpdf[,c('x','y')]) ] <- tmpdf$a
cbind is also useful for assembly of arguments to the matrix-`[<-`
function:
tmpdf <- data.fram
tmpdf <- data.frame( x = c(1,2,3), y=c(2,3,1), a=c(10,20,30) )
mymat <- matrix(0, ncol=3, nrow=3)
mymat[ as.matrix(tmpdf[,c('x','y')]) ] <- tmpdf$a
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111
> -Original Message-
> F
index m as a vector and do the assignment in one step
i <- df$row + (df$col-1)*nrow(m)
m[i] <- df$a
or something along those lines.
-Whit
On Tue, Dec 7, 2010 at 1:31 PM, Cutler, Gene wrote:
> I have a data frame with three columns, x, y, and a. I want to create a
> matrix from these values
4 matches
Mail list logo