如何重命名plyr的** ply函数的输出行/列? [英] How can I rename the output rows/cols of **ply functions from plyr?
问题描述
我想在plyr包的**ply
函数ldply
中声明行/列的输出名称.
I would like to state the row/col output names in a **ply
function, ldply
, from the plyr package.
例如
我有一个列表foo
,我想将其转换为data.frame
并用signif()
截断有效数字
I have a list, foo
, that I want to convert to a data.frame
and truncate significant digits with signif()
foo <- list(var.a = runif(3), var.b = runif(3), var.c=runif(3))
我现在拥有的是
q <- ldply(foo, signif, 2)
colnames(dq)[1] <- c('id', 'q1', 'q2','q3')
rownames(dq) <- dq$id
有没有更简单的方法?
之前的两个问题询问了如何使用plyr重命名行和 cols ,并使用plyr,但我认为我的问题有所不同.名称可以与另一个功能同时声明(或者如果我正确执行的话)吗?这是一个有价值的功能请求吗?
Two previous questions have asked how to use plyr to rename rows and cols using plyr, but I think my question is different. Can the names be stated at the same time as another function (or if I am doing this correctly)? Is this a worthwhile feature request?
推荐答案
您必须在某个地方给出名称,例如在内部调用的函数上,例如
You have to give names somewhere, either on the function being called inside as eg in
R> ldply(foo, function(l) c(a=signif(l[1], 2), b=signif(l[2], 2),
+ c=signif(l[3], 2)))
.id a b c
1 var.a 0.50 0.72 0.27
2 var.b 0.82 0.38 0.24
3 var.c 0.13 0.27 0.81
R>
或者您在通话后做了什么.
or has you did after the call.
我经常使用的另一个选项是在匿名工作程序函数中显式创建单行data.frame
.然后*dply()
等将它们简单地整理为一个data.frame
.这足以满足我的口味.
Another option I frequently use is to explicitly create one-row data.frame
in the anonymous worker function. *dply()
et al then simply collated these into a single data.frame
. That works well enough for my tastes.
这篇关于如何重命名plyr的** ply函数的输出行/列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!