ggplot根据行和列选择数据

时间:2017-04-28 22:49:19

标签: r

数据如下:

     Genres     Class Track_Num
1     'pop' unpopular     24786
2    'rock' unpopular     18792
3     'pop'   popular     11631
4     'blue'  popular      6000
 ............

现在我只想选择流派(比如'pop'),其不受欢迎和流行的Track_Num的总和是> 500,我该怎么办? 感谢

1 个答案:

答案 0 :(得分:0)

使用dplyr,您可以group_by(Genres)创建一个新变量sum(Track_Num),然后filter()

library(dplyr)

# Recreating your dataframe
music <- data.frame(Genres = c('pop', 'rock', 'pop', 'blue'),
                    Class = c('unpopular', 'popular', 'popular', 'popular'),
                    Track_Num = c(24786, 18792, 11631, 6000))

music %>% 
  group_by(Genres) %>% 
  summarise(Total_Track_Num = sum(Track_Num)) %>% 
  filter(Total_Track_Num > 500)

#   Genres Total_Track_Num
#   <fctr>           <dbl>
# 1   blue            6000
# 2    pop           36417
# 3   rock           18792