I think you will be okay if you change
one line to:

defMat<-sapply(defData[,-1, drop=FALSE], function(x) breakUpFun(freq, x))

In your example that doesn't work you are
ending up with a vector rather than a one
column data frame.

Pat


On 07/01/2014 17:44, Keith S Weintraub wrote:
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



--

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to