[R] Selecting section of matrix

mdvaan mathijsdevaan at gmail.com
Wed Aug 17 17:11:09 CEST 2011


That worked great, thanks! Now that I have created list h (see below), I
would like to use the selections made in h to make new selections in list c
(see below). List c needs to get the exact same shape as h, so that `8026`in
1997 (c$`1997`$`8026`) looks like this: 

$`1997`$`8026`
      B
B      8025 8026 8029
  8025   1.0000000 0.7739527 0.9656091
  8026   0.7739527 1.0000000 0.7202771
  8029   0.9656091 0.7202771 1.0000000

Thank you very much for your help!

library(zoo) 

DF1 = data.frame(read.table(textConnection("    B  C  D  E  F  G 
8025  1995  0  4  1  2 
8025  1997  1  1  3  4 
8026  1995  0  7  0  0 
8026  1996  1  2  3  0 
8026  1997  1  2  3  1 
8026  1998  6  0  0  4 
8026  1999  3  7  0  3 
8027  1997  1  2  3  9 
8027  1998  1  2  3  1 
8027  1999  6  0  0  2 
8028  1999  3  7  0  0 
8029  1995  0  2  3  3 
8029  1998  1  2  3  2 
8029  1999  6  0  0  1"),head=TRUE,stringsAsFactors=FALSE)) 
 
a <- read.zoo(DF1, split = 1, index = 2, FUN = identity) 
sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA 
b <- rollapply(a, 3,  sum.na, align = "right", partial = TRUE) 
newDF <- lapply(1:nrow(b), function(i) 
	      prop.table(na.omit(matrix(b[i,], nc = 4, byrow = TRUE, 
		dimnames = list(unique(DF1$B), names(DF1)[-1:-2]))), 1)) 
names(newDF) <- time(a) 
c<-lapply(newDF, function(mat) tcrossprod(mat / sqrt(rowSums(mat^2)))) 

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))

years<-c(1997:1999)
for (t in 1:length(years))
	{
	year=as.character(years[t])
	h[[year]]<-sapply(colnames(h[[year]]), function(var)
h[[year]][h[[year]][,var]>0, h[[year]][var,]>0])
	}


David Winsemius wrote:
> 
> On Aug 15, 2011, at 6:09 AM, mdvaan wrote:
> 
>> 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
>>
>  > tst <- h$`1999`
>  > tst[tst[,"8025"]>0, tst["8025",]>0]
>        B
> B      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
> 
>  > tst[tst[,"8028"]>0, tst["8028",]>0]
>        B
> B      8028 8029
>    8028    1    1
>    8029    1    1
> 
> And to do it programmatically:
> 
> sapply( colnames(tst), function(var) tst[tst[,var]>0, tst[var,]>0])
> 
> -- 
> David.
>>
>>
>>
>> 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.
>>
>> ______________________________________________
>> 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.
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
> 
> ______________________________________________
> 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.
> 


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



More information about the R-help mailing list