使用dplyr将函数应用于分组变量?

时间:2015-06-01 19:18:11

标签: r plyr dplyr

Learning R帖子根据变量组将一个函数应用于列:

数据如下所示:

$query = 'SELECT p.PO, p.Date FROM po_10152796 AS p LIMIT 10';

我可以使用此函数按变量重新缩放:

 NAME, variable, value
 1   , GROUP1, 10
 2   , GROUP1, 20
 3   , GROUP2, 20
 4   , GROUP2, 30

如何使用dplyr包执行相同的计算?我试过了:

nba.m <- ddply(nba.m, .(variable), transform, rescale = rescale(value))

然而,这会扩大整个&#34;值&#34;没有按变量分组的列。谢谢, 马特

1 个答案:

答案 0 :(得分:0)

这可能是由plyr在dplyr中屏蔽对象引起的。这就是您应该在致电library(dplyr)后始终致电library(plyr)的原因。如果不这样做,应该引出这个警告:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
You have loaded plyr after dplyr - this is likely to cause problems.
If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
library(plyr); library(dplyr)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Attaching package: ‘plyr’

The following objects are masked from ‘package:dplyr’:

    arrange, count, desc, failwith, id, mutate, rename, summarise, summarize

所以你也可能遇到与其他这些功能类似的问题。通过在dplyr之前附加plyr或者@akrun指出来解决它:明确地引用你想要的包中的函数,在这种情况下通过前缀dplyr::

请注意,您可以使用search()检查附加软件包的顺序(在这种情况下,我在 plyr之后附加了dplyr ):

search()
 [1] ".GlobalEnv"        "package:dplyr"     "package:plyr"      "tools:rstudio"     "package:stats"     "package:graphics"  "package:grDevices" "package:utils"    
 [9] "package:datasets"  "package:methods"   "Autoloads"         "package:base"