Leave only those rows in matrices which have equal elements at certain column

前端 未结 4 983
一向
一向 2021-01-28 00:56

Let me show an example. Consider we have 3 tables (focus on columns N):

   Table 1         Table 2        Table 3
-------------   -------------   -------------
           


        
4条回答
  •  盖世英雄少女心
    2021-01-28 01:31

    Here's a more functional way that will work with any list of tables. First we extract all the 'N' columns and then get the intersection of all these values. Then we just filter each of the tables.

    library('tidyverse')
    
    tables <- list(Table1, Table2, Table3)
    
    common <- tables %>%
      map('N') %>%
      reduce(intersect)
    
    tables %>%
      map(filter, N %in% common)
    # [[1]]
    #    N Values
    # 1  5      1
    # 2 10      2
    # 3 15      3
    # 
    # [[2]]
    #    N Values
    # 1  5     -1
    # 2 10     -3
    # 3 15     -4
    # 
    # [[3]]
    #    N Values
    # 1  5      1
    # 2 10      5
    # 3 15      3
    

提交回复
热议问题