[R] Selections in lists
mdvaan
mathijsdevaan at gmail.com
Thu Aug 25 17:24:12 CEST 2011
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!
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.
More information about the R-help
mailing list