0
votes

This Learning R post applies a function to a column based on variable group:

The data looks like this:

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

I can use this function to rescale by variable:

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

How would I perform this same calculation using the dplyr package? I've tried:

nba.m <- nba.m %>%group_by(variable) %>% mutate(rescale=rescale(as.numeric(value)))

However this scales the entire "value" column without grouping by variable. Thanks, Matt

1
Try dplyr::mutate(rescale=akrun
Where does rescale() come from? It seems to work fine with scale: nba.m %>%group_by(variable) %>% mutate(rescale=scale(as.numeric(value))) What output do you get? Do you have both dplyr and plyr loaded? Did you load plyr first?MrFlick
@MrFlick After I load ggplot2, some hadleyverse "scales" package is loaded which has a rescale function; I guess that's it. Must be, since ggplot2 is in the title of the post the OP linked toFrank

1 Answers

0
votes

This is probably caused by the masking of objects in dplyr by plyr. This is why you should always call library(dplyr) after calling library(plyr). Failing to do so should elicit this warning:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
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

So you could run into similar problems with these other functions too. Solve it by attaching plyr before dplyr, or as @akrun points out: explicitly referring to the function from the package you want, in this case by prefixing with dplyr::.

Note that you can check the order of attached packages with search() (in this case I attached dplyr after plyr):

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"