获取R中的组序列 [英] get sequence of group in R

查看:24
本文介绍了获取R中的组序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经完成了我需要的工作,但我相信有更好的方法来做到这一点

So I have already done what I need but I am sure that there is a better way to do that

library(tidyverse)
library(schrute)
office <- schrute::theoffice

top_3_lines_per_episode <- office %>% 
  group_by(season,episode,episode_name,imdb_rating) %>% 
  count(character) %>%
  top_n(3, n) %>% ungroup()

epi_num<-top_3_lines_per_episode %>% 
  select(episode_name) %>% 
  unique() %>% 
  mutate(episode_num=row_number()) 

top_3_lines_per_episode %>% 
  inner_join(epi_num)

我想生成列epi_num 使用上面完成.一个更简单的方法来做到这一点.我查看了来自 SO 的 group_indices,但我认为那已被弃用.需要更好的方法,最好在 tidyverse 中.

I want to generate column epi_num which gets done using above. A simpler way to do that. I have look at group_indices from SO but I think thats derecated. Need a better way preferably in tidyverse.

推荐答案

我不知道 group_indices 已被弃用,事实上它在这里似乎是一个完美的选择.

I am not aware of group_indices being deprecated in fact it seems to be a perfect option here.

library(dplyr)

office %>% 
  group_by(season,episode,episode_name,imdb_rating) %>% 
  count(character) %>%
  top_n(3, n) %>%
  ungroup %>%
  mutate(episode_num = group_indices(., season,episode,episode_name,imdb_rating))

<小时>

另一种选择是将列与 unite 结合,然后 match 得到 episode_num.


Another option is to combine the columns with unite and then match to get episode_num.

office %>% 
  group_by(season,episode,episode_name,imdb_rating) %>% 
  count(character) %>%
  top_n(3, n) %>%
  ungroup %>%
  tidyr::unite(temp, season,episode,episode_name,imdb_rating, remove = FALSE) %>%
  mutate(episode_num = match(temp, unique(temp))) %>%
  select(-temp)

这篇关于获取R中的组序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆