getting started with plyr `m*ply` but unable to reproduce examples
Trying to learn plyr, I have gotten stuck trying to reproduce code from the introductory guide.
The guide says that the code is in a file plyr.r
, but not where I can find this file.
But reproducing one of the first examples seemed easy enough, so I decided to give it a try:
dat <- data.frame(c(10,100,50), mean=c(5,5,10), sd=c(1,2,1))
maply(dat, rnorm)
and I get this error:
Error in function (..., na.last = TRUE, decreasing = FALSE) :
unimplemented type 'list' in 'orderVector1'
trying
dat <- cbind(c(10,100,50), mean=开发者_如何学Pythonc(5,5,10), sd=c(1,2,1))
maply(dat, rnorm)
gives
Error: Results must have the same dimensions.
questions:
- what am I doing wrong?
- where can I find plyr.r? (it is not here)
The data frame you made has a header (col.names) which is not compatible with the rnorm function. See:
> dat <- data.frame(c(10,100,50), mean=c(5,5,10), sd=c(1,2,1))
> dat
c.10..100..50. mean sd
1 10 5 1
2 100 5 2
3 50 10 1
And the m*pply function do not know what to do with the 'c.10..100..50...' column.
As you can see in the docs (?mdply
), the following example works like a charm:
> mdply(data.frame(mean = 1:5, sd = 1:5), rnorm, n = 2)
mean sd V1 V2
1 1 1 0.09919179 0.6083586
2 2 2 0.92787891 -0.1139743
3 3 3 2.21236781 0.8029677
4 4 4 4.16506428 9.2477373
5 5 5 1.26558507 12.0633377
If you really want different number of observations with the different parameters, you should not use mdply, because the matrix/data.frame must have the same number of columns. Insted use mlply
, e.g.:
> mlply(data.frame(n=1:5, mean = 1:5, sd = 1:5), rnorm)
$`1`
[1] 1.053083
$`2`
[1] -1.650090 2.239547
$`3`
[1] -0.94697908 -1.11479730 -0.03467497
$`4`
[1] 6.427796 1.482655 1.436822 -5.993420
$`5`
[1] 4.557689 6.217015 2.105255 -1.309664 -2.969184
attr(,"split_type")
[1] "array"
attr(,"split_labels")
n mean sd
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
精彩评论