On Aug 25, 2011, at 11:24 AM, mdvaan wrote:

Hi,

I have produced a list g and I would like to reduce the amount of
information contained in each object in g.
For each matrix I would like to keep the values where the column name equals
g[year][[1]][[x]] and the row names equals g[year][[1]][[-x]]. So in
g$`1999`$`8029`, year = 1999 and x = 8029. I have been experimenting with
the subset function, but have been unsuccesful. Thanks for your help!


I suspect this is because you g object does not have the structure that you imagine for it:

> str(g)
List of 3
 $ 1997: num [1:4, 1:4] 3 2 1 1 2 3 0 1 1 0 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ B: chr [1:4] "8025" "8026" "8027" "8029"
  .. ..$ B: chr [1:4] "8025" "8026" "8027" "8029"
 $ 1998: num [1:4, 1:4] 2 1 1 0 1 3 1 1 1 1 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ B: chr [1:4] "8025" "8026" "8027" "8029"
  .. ..$ B: chr [1:4] "8025" "8026" "8027" "8029"
 $ 1999: num [1:5, 1:5] 2 1 1 0 0 1 3 2 0 1 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ B: chr [1:5] "8025" "8026" "8027" "8028" ...
  .. ..$ B: chr [1:5] "8025" "8026" "8027" "8028" ...

The things you think are list indices are really in the 'dimnames' attributes of the objects.


--
David
The result for g$`1999`$`8029` should be:

$`1999`$`8029`
     B
B      8029
 8026    1
 8027    1
 8028    1

The result for g$`1999`$`8028` should be:

$`1999`$`8028`
     B
B      8028
 8029    1


The result for g$`1999`$`8027` should be:

$`1999`$`8027`
     B
B       8027
 8025     1
 8026     2
 8029     1


Example:

DF = 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(DF[DF$C %in% y, 1:2]))
years <- sort(unique(DF$C))
f <- as.data.frame(embed(years, 3))
g<-lapply(split(f, f[, 1]), e)
years<-names(g)
for (t in seq(years))
       {
       year=as.character(years[t])
       g[[year]]<-sapply(colnames(g[[year]]), function(var)
g[[year]][g[[year]][,var]>0, g[[year]][var,]>0])
       }

--
View this message in context: 
http://r.789695.n4.nabble.com/Selections-in-lists-tp3768562p3768562.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to