[R] Multiply list objects

mdvaan mathijsdevaan at gmail.com
Tue Jun 14 08:03:43 CEST 2011


Hi,

I am trying to use the objects from the list below to create more objects.
For each year in h I am trying to create as many objects as there are B's
keeping only the values of B. Example for 1999: 

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

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

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

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

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

Any suggestions? You help is very much appreciated!

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)
h<-lapply(g, function (x) ifelse(x>0,1,0))

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



More information about the R-help mailing list