简体   繁体   中英

Rescale for NBA heatmap: dplyr equivalent to plyr function?

There is this great example of how to use ggplot2 to create a heat map the 'R; way: Rheatmap which provides a link to the raw data and the source code.

There was a followup using ggplot2: ggplot2 which lays out the ggplot2 code.

At key points the ggplot2 code uses reshape2 and plyr.

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

My goal is to duplicate these calculations using tidyr and dplyr.

 nba.m <- melt(nba)

has a tidyr equivalent in:

 nba.g <- gather(nba, Name) 

What is the dplyr equivalent to this line?

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

eipi10 kindly suggested

nba.m2 <- nba.m %>%group_by(Name) %>% mutate(rescale=rescale(value))

However, it looks like the rescale calculation is not occuring in quite the same way:

> head(nba.m)
        Name variable value   rescale
1   Dwyane Wade         G    79 0.9473684
2  LeBron James         G    81 0.9824561
3   Kobe Bryant         G    82 1.0000000
4 Dirk Nowitzki         G    81 0.9824561
5 Danny Granger         G    67 0.7368421
6  Kevin Durant         G    74 0.8596491
> head(nba.m2)
Source: local data frame [6 x 4]
Groups: Name

        Name Name.1 value   rescale
1   Dwyane Wade       G    79 0.9634146
2  LeBron James       G    81 0.9878049
3   Kobe Bryant       G    82 1.0000000
4 Dirk Nowitzki       G    81 0.9878049
5 Danny Granger       G    67 0.8170732
6  Kevin Durant       G    74 0.9024390
> 

What is missing?

Thanks, Matt

I think you need write dplyr::mutate , not mutate .

I presume you loaded plyr and dplyr in the same session. dplyr and plyr are conflict the following objects: arrange, count, desc, failwith, id, mutate, rename, summarise, summarize

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM