Hi:
Here's one way, but not especially elegant. The idea is to initialize a
matrix of zeros,
produce an index matrix from two of your objects and then assign the third
object
to the specified indices.
x <- matrix(0, nrow = 3, ncol = 10)
row <- rep(1:nrow(x), unname(sapply(y.covar, nrow)))
row
[1] 1 2 3 3
col <- unname(unlist(y.covar))
col
[1] 5 6 2 3
idx <- cbind(row, col)
x[idx] <- unname(unlist(mass.func))
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0.0 0.0 0 1 0 0 0 0 0
[2,] 0 0.0 0.0 0 0 1 0 0 0 0
[3,] 0 0.5 0.5 0 0 0 0 0 0 0
HTH,
Dennis
On Mon, May 24, 2010 at 11:16 AM, Kathryn Lord
<[email protected]>wrote:
> Dear R users,
>
> I am trying to make this (3 by 10) matrix A
>
> --A----------------------------------------------------
>
> 0 0 0 0 1 0 0 0 0 0
> 0 0 0 0 0 1 0 0 0 0
> 0 0.5 0.5 0 0 0 0 0 0 0
>
> -------------------------------------------------------
>
> from "mass.func"
>
> --mass.func-------------------------------------------
> > mass.func
> $`00`
> prop
> 5
> 1
>
> $`10`
> prop
> 6
> 1
>
> $`11`
> prop
> 2 3
> 0.5 0.5
> ---------------------------------------------------------
>
> which means that
>
> A[1,5] = 1
> A[2,6] =1
> A[3,2] = A[3,3] = 0.5
>
> otherwise, zero.
>
> Any suggestion will be greatly appreciated.
>
> Regrads,
>
> Kathryn Lord
>
>
> p.s.
>
> Here is R code.
>
> ------------------------------------------------------------------
>
> > dat <- as.data.frame(matrix( c( 2, 1, 1, 3, 1, 1, 6, 1, 0, 5, 0, 0), 4,
> 3,
> byrow=T))
> > covar <- apply(dat[,-1],1,paste,collapse='')
> > sp.dat <- split(dat,covar)
> > y.covar <- lapply(sp.dat, "[",1)
> >
> > prop <- function(prop) { table(prop)/sum(table(prop)) }
> > mass.func <- lapply(y.covar,prop)
> > mass.func
> $`00`
> prop
> 5
> 1
>
> $`10`
> prop
> 6
> 1
>
> $`11`
> prop
> 2 3
> 0.5 0.5
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] 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.
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.