[R] How to "apply" correctly.
Keith S Weintraub
kw1958 at gmail.com
Tue Jan 7 18:44:42 CET 2014
Folks,
# I have the following function:
breakByFreq<-function(freq, defData) {
breakUpFun<-function(freq, defs) {
if(freq != 1) {
defs<-diff(c(0, defs))
defs<-cumsum(rep(defs/freq, each = freq))
}
defs
}
defMat<-sapply(defData[,-1], function(x) breakUpFun(freq, x))
data.frame(Year = 1:nrow(defMat), defMat)
}
# And this data (year column and then 2 columns of data):
dum<-structure(list(Year = 1:10,
c1 = c(0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1),
c2 = c(0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2)),
.Names = c("Year", "c1", "c2"), row.names = c(NA, -10L), class = "data.frame")
# This works:
breakByFreq(1, dum)
# This doesn't:
breakByFreq(1, dum[,-3])
# How do I use and choose the appropriate apply function to make this work when there is one and or more columns to be "processed".
Thanks for your time,
KW
--
More information about the R-help
mailing list