[R] Selecting section of matrix

mdvaan mathijsdevaan at gmail.com
Mon Aug 15 15:09:17 CEST 2011


Hi,

I have a question concerning the selection of data. Let's say that given
list h created below, I would like to select a section of the 1999 matrix.
For a case (rownames and colnames) I would like to select the cells that
have a value > 0. So for case 8025

       8025 8026 8027
8025    1    1    1
8026    1    1    1
8027    1    1    1


And for case 8028

       8028 8029
8028    1    1
8029    1    1



DF2 = data.frame(read.table(textConnection("  A  B  C
80  8025  1995
80  8026  1995
80  8029  1995
81  8026  1996
82  8025  1997
82  8026  1997
83  8025  1997
83  8027  1997
90  8026  1998
90  8027  1998
90  8029  1998
84  8026  1999
84  8027  1999
85  8028  1999
85  8029  1999"),head=TRUE,stringsAsFactors=FALSE))

e <- function(y) crossprod(table(DF2[DF2$C %in% y, 1:2])) 
years <- sort(unique(DF2$C)) 
f <- as.data.frame(embed(years, 3)) 
g<-lapply(split(f, f[, 1]), e)
h<-lapply(g, function (x) ifelse(x>0,1,0))# These are the adjacency matrices
per year
h

Thanks very much!

--
View this message in context: http://r.789695.n4.nabble.com/Selecting-section-of-matrix-tp3744570p3744570.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list