当前位置:   article > 正文

数据分析:筛选多组交集特征

数据分析:筛选多组交集特征

介绍

有时候需要在多个组间筛选它们的交集特征,本文利用R语言实现该目的

加载R包

library(UpSetR)
library(tidyverse)
  • 1
  • 2

Upset画图

movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
                   header = T, sep = ";")
movies_list <- list(
  Action = movies %>%
    dplyr::filter(Action == 1) %>%
    dplyr::pull(Name),
  Adventure = movies %>%
    dplyr::filter(Adventure == 1) %>%
    dplyr::pull(Name),
  Children = movies %>%
    dplyr::filter(Children == 1) %>%
    dplyr::pull(Name),
  Comedy = movies %>%
    dplyr::filter(Comedy == 1) %>%
    dplyr::pull(Name),
  Crime = movies %>%
    dplyr::filter(Crime == 1) %>%
    dplyr::pull(Name),
  Documentary = movies %>%
    dplyr::filter(Documentary == 1) %>%
    dplyr::pull(Name)  
)

movies_pl <- UpSetR::upset(
  data = fromList(movies_list),
  nsets = 3, 
  sets = c("Action", "Adventure", "Children", 
           "Comedy", "Crime", "Documentary"),
  order.by = "freq",
  main.bar.color = "gray10",
  sets.bar.color = "gray",
  matrix.color = "gray10",
  mainbar.y.label = "NO. of movies",
  sets.x.label = "NO. of movies")

movies_pl
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

在这里插入图片描述

判断交集特征

  • 去冗余变量 df_uniq_movie

  • 分组变量标签 df_group_movie


df_uniq_movie <- data.frame(feature = unique(unlist(movies_list)))
df_group_movie <- lapply(movies_list, function(x){
  data.frame(feature = x)
}) %>% 
  dplyr::bind_rows(.id = "Sequence")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 给变量打上交集标签
df_int_movie <- lapply(df_uniq_movie$feature, function(x){
  intersection <- df_group_movie %>% 
    dplyr::filter(feature == x) %>% 
    dplyr::arrange(Sequence) %>% 
    dplyr::pull(Sequence) %>% 
    paste0(collapse = "|")
  # build the dataframe
  return(data.frame(feature = x, int = intersection))
}) %>% 
  dplyr::bind_rows()

head(df_int_movie)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/504298
推荐阅读
相关标签
  

闽ICP备14008679号