为了学习plyr,我被困在试图从介绍性指南复制代码。
指南说代码在plyr.r文件中,但我在哪里可以找到这个文件。
但是复制最初的一个例子似乎很容易,所以我决定尝试一下:

dat <- data.frame(c(10,100,50), mean=c(5,5,10), sd=c(1,2,1))
maply(dat, rnorm)我得到了一个错误:
Error in function (..., na.last = TRUE, decreasing = FALSE) :
unimplemented type 'list' in 'orderVector1'正在尝试
dat <- cbind(c(10,100,50), mean=c(5,5,10), sd=c(1,2,1))
maply(dat, rnorm)给出
Error: Results must have the same dimensions.问题:
发布于 2011-01-06 17:27:18
您创建的数据帧有一个标头(col.names),它与rnorm函数不兼容。请参见:
> 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而m*pply函数不知道如何处理“c.10..100.50.”列。
正如您在docs (?mdply)中所看到的,下面的示例工作起来很有魅力:
> 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如果您确实希望使用不同参数的不同数量的观察,则不应该使用mdply,因为矩阵/data.framework必须有相同的列数。Insted使用mlply,例如:
> 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 5https://stackoverflow.com/questions/4617419
复制相似问题