[R] Befuddled by ddply
Keith S Weintraub
kw1958 at gmail.com
Thu Oct 2 16:19:14 CEST 2014
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
More information about the R-help
mailing list