Skip to content
Prev 258193 / 398502 Next

Yearly aggregates and matrices

Hi,

Is there an alternative to "z <- read.zoo(DF, split = 2, index = 3, FUN =
identity)" and "r <- rollapply(z, 3,  sum.na, align = "right", partial =
TRUE)"? I am trying to use the following script in which the split data (B)
contains about 300000 unique cases and obviously I am getting an allocation
error. Thanks!

# devel version of zoo
install.packages("zoo", repos = "http://r-forge.r-project.org")

DF = data.frame(read.table(textConnection(" ?A ?B ?C ?D ?E ?F
1 a ?1995 ?0 ?4 ?1
2 a ?1997 ?1 ?1 ?3
3 b ?1995 ?3 ?7 ?0
4 b ?1996 ?1 ?2 ?3
5 b ?1997 ?1 ?2 ?3
6 b ?1998 ?6 ?0 ?0
7 b ?1999 ?3 ?7 ?0
8 c ?1997 ?1 ?2 ?3
9 c ?1998 ?1 ?2 ?3
10 c ?1999 ?6 ?0 ?0
11 d ?1999 ?3 ?7 ?0
12 e ?1995 ?1 ?2 ?3
13 e ?1998 ?1 ?2 ?3
14 e ?1999 ?6 ?0 ?0"),head=TRUE,stringsAsFactors=FALSE))


library(zoo)

z <- read.zoo(DF, split = 2, index = 3, FUN = identity)

sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA
r <- rollapply(z, 3,  sum.na, align = "right", partial = TRUE)

newDF <- lapply(1:nrow(r), function(i)
	prop.table(na.omit(matrix(r[i,], nc = 4, byrow = TRUE,
		dimnames = list(unique(DF$B), names(DF)[-2:-3]))[, -1]), 1))
names(newDF) <- time(z)

lapply(newDF, function(mat) tcrossprod(mat / sqrt(rowSums(mat^2))))
Gabor Grothendieck wrote:
--
View this message in context: http://r.789695.n4.nabble.com/Yearly-aggregates-and-matrices-tp3438140p3478997.html
Sent from the R help mailing list archive at Nabble.com.