[R] Befuddled by ddply

Keith S Weintraub kw1958 at gmail.com
Fri Oct 3 15:14:47 CEST 2014


Dennis,
Thanks for the help. I am using colwise now in a couple of places.
Best,
KW


On Oct 2, 2014, at 12:26 PM, Dennis Murphy <djmuser at gmail.com> wrote:

> plyr::colwise(defCurveBreak, y = 4)(mdf)
> 
> It took me a few minutes to realize that defCurveBreak() took a vector
> as its first argument; then it made more sense.
> 
> Dennis
> 
> On Thu, Oct 2, 2014 at 7:19 AM, Keith S Weintraub <kw1958 at gmail.com> wrote:
>> Folks,
>> 
>> I have the following data:
>> 
>> mdf<-structure(list(a = 1:3, b = c(10, 20, 30)), .Names = c("a", "b"
>> ), row.names = c(NA, -3L), class = "data.frame")
>> 
>> And function:
>> 
>> defCurveBreak<-function(x, y) {
>>  cumsum(rep(diff(c(0, x)), each = y)/y)
>> }
>> 
>> lapply'ing to get the result "foo"
>> 
>> foo<-data.frame(lapply(mdf, function(x, y) defCurveBreak(x,y), 4))
>>> foo
>>      a    b
>> 1  0.25  2.5
>> 2  0.50  5.0
>> 3  0.75  7.5
>> 4  1.00 10.0
>> 5  1.25 12.5
>> 6  1.50 15.0
>> 7  1.75 17.5
>> 8  2.00 20.0
>> 9  2.25 22.5
>> 10 2.50 25.0
>> 11 2.75 27.5
>> 12 3.00 30.0
>> 
>> Which all works fine.
>> 
>> I was wondering is there a way to do this using ddply? Is there a reason to do this using ddply rather than the above idiom?
>> 
>> I spent a bunch of time trying to figure out how to set it up in ddply (is that the wrong tool?) to no avail.
>> 
>> Thanks for your time,
>> Best,
>> KW
>> 
>> ______________________________________________
>> R-help at 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.



More information about the R-help mailing list