Hi,
I have a long data file with time data that change to wide format using reshape.
The data contain Values and Factors. Some values are missing but can be obtained
by multiplying value of year T-1 with Factor of year T. Sometimes, multiple
succesive years have no values, so the calculated values need to be calculated
sequentially.
# sample data.
DF <- data.frame(Var=rep(letters, 10),
Fac=rep(runif(26), 10),
Val=rep(runif(26), 10),
Year=rep(2000:2009, 26))
DF[as.numeric(rownames(DF)) %% 3 == 0,"Val"] <- NA # make some
holes
DF2 <- cast(melt(DF, id=c("Var", "Year")), ... ~ variable
+ Year, fun=mean,
na.rm=T)
# my attempt
library(reshape)
prev <- grep("Val_", names(DF2)) - 1
this <- grep("Fac_", names(DF2))
DF3 <- DF2
DF3[, prev] <- mapply("*", DF2[, this], DF2[, prev])
This doesn't work. Another option would be to use two loops for cols and
rows,
but I didn't get that to work either :-(
Suggestions for clean code, anyone?
Thank you in advance!
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public
order, irrigation, roads, a fresh water system, and public health, what have the
Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[[alternative HTML version deleted]]