在dplyr中选择两个变量共同满足条件的行

时间:2017-04-07 12:20:12

标签: r dplyr

我有两个数据集,big_tablesmall_table

    big_table = structure(list(FXW = structure(c(7L, 8L, 3L, 7L, 7L, 
1L, 6L, 1L, 3L, 2L, 6L, 5L, 3L, 3L, 7L, 5L, 2L, 3L, 2L, 6L, 5L, 
1L, 6L, 2L, 4L, 8L, 3L, 1L, 7L, 5L, 3L, 2L, 2L, 2L, 1L, 2L, 1L, 
5L, 4L, 1L, 4L, 6L, 3L, 3L, 5L, 3L, 8L, 3L, 8L, 1L, 2L, 1L, 8L, 
1L, 6L, 3L, 5L, 2L, 6L, 7L, 6L, 8L, 6L, 8L, 4L, 1L, 6L, 7L, 7L, 
1L, 2L, 4L, 4L, 6L, 1L, 4L, 1L, 1L, 2L, 4L, 2L, 7L, 4L, 6L, 7L, 
2L, 2L, 1L, 2L, 7L, 5L, 2L, 2L, 8L, 1L, 2L, 5L, 2L, 8L, 2L), .Label = c("AA", 
"AF", "AG", "AH", "AI", "BG", "EH", "FF"), class = "factor"), 
    CP = c("CAD/AOA", "CHF/AUD", "CAD/ATS", "USD/CDF", "JPY/CDF", 
    "EUR/AUD", "JPY/CNY", "EUR/AOA", "CHF/CDF", "CAD/CDF", "USD/CNY", 
    "EUR/AUD", "JPY/AUD", "USD/CDF", "JPY/ATS", "JPY/CDF", "CAD/CNY", 
    "GBP/ATS", "CHF/AOA", "GBP/AUD", "USD/CNY", "JPY/CDF", "CHF/AOA", 
    "GBP/ATS", "EUR/AUD", "GBP/BDT", "EUR/AOA", "GBP/ATS", "JPY/CNY", 
    "CAD/ATS", "CAD/CNY", "CHF/CDF", "EUR/CNY", "JPY/BDT", "GBP/BDT", 
    "EUR/AUD", "CAD/CDF", "CAD/ATS", "GBP/ATS", "EUR/AUD", "JPY/ATS", 
    "GBP/BDT", "EUR/ATS", "CAD/CNY", "CHF/AUD", "CHF/CDF", "CAD/ATS", 
    "CAD/CDF", "CAD/CNY", "JPY/CDF", "JPY/BDT", "USD/BDT", "EUR/CNY", 
    "CHF/CDF", "GBP/CNY", "CHF/CNY", "CHF/CDF", "CHF/AUD", "CAD/AUD", 
    "GBP/ATS", "CHF/BDT", "JPY/AUD", "CHF/BDT", "CAD/BDT", "CHF/AUD", 
    "CAD/ATS", "CAD/CNY", "CAD/ATS", "JPY/BDT", "GBP/CNY", "GBP/AUD", 
    "USD/AOA", "USD/AOA", "GBP/AOA", "GBP/AOA", "EUR/CNY", "CAD/CNY", 
    "JPY/CDF", "CHF/CNY", "CAD/BDT", "USD/CNY", "CHF/AOA", "GBP/AUD", 
    "USD/AUD", "CHF/BDT", "JPY/ATS", "JPY/ATS", "GBP/BDT", "JPY/AOA", 
    "JPY/AUD", "GBP/CDF", "CHF/CDF", "USD/ATS", "CHF/CNY", "CHF/BDT", 
    "CAD/ATS", "EUR/AUD", "CHF/CDF", "JPY/BDT", "GBP/AUD")), .Names = c("FXW", 
"CP"), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"
))



        small_table = structure(list(SWH = structure(c(8L, 7L, 1L, 5L, 3L, 7L, 8L, 
6L, 6L, 4L), .Label = c("AA", "AF", "AG", "AH", "AI", "BG", "EH", 
"FF"), class = "factor"), SCP = structure(c(5L, 23L, 19L, 36L, 
12L, 28L, 14L, 7L, 10L, 21L), .Label = c("CAD/AOA", "CAD/ATS", 
"CAD/AUD", "CAD/BDT", "CAD/CDF", "CAD/CNY", "CHF/AOA", "CHF/ATS", 
"CHF/AUD", "CHF/BDT", "CHF/CDF", "CHF/CNY", "EUR/AOA", "EUR/ATS", 
"EUR/AUD", "EUR/BDT", "EUR/CDF", "EUR/CNY", "GBP/AOA", "GBP/ATS", 
"GBP/AUD", "GBP/BDT", "GBP/CDF", "GBP/CNY", "JPY/AOA", "JPY/ATS", 
"JPY/AUD", "JPY/BDT", "JPY/CDF", "JPY/CNY", "USD/AOA", "USD/ATS", 
"USD/AUD", "USD/BDT", "USD/CDF", "USD/CNY"), class = "factor")), .Names = c("SWH", 
"SCP"), row.names = c(256L, 238L, 19L, 179L, 83L, 243L, 265L, 
186L, 189L, 128L), class = "data.frame")

我正在尝试使用dplyr来选择big_table行的子集,(big_table$FXW, big_table$CP) 联合(small_table$SWH, small_table$SCP)

2 个答案:

答案 0 :(得分:3)

翻译您的描述的另一种方式(" (big_table$FXW, big_table$CP) are in (small_table$SWH, small_table$SCP)")将是

big_table[do.call(paste, big_table) %in% do.call(paste, small_table),]

#or to specify the columns you want to compare,

big_table[do.call(paste, big_table[,c('FXWAREHOUSE', 'CP')]) %in% 
                                    do.call(paste, small_table[,c('SWH', 'SCP')]),]

答案 1 :(得分:2)

我们可以tiles : { module1 : [ { "header":"Data Peserta", "subheader":"Kepesertaan", "icon":"sap-icon://log", "route":"datapeserta" }, { "header":"Header1", "subheader":"Kepesertaan", "icon":"sap-icon://delete", "route":"first" }, { "header":"Header1", "subheader":"Kepesertaan", "icon":"sap-icon://line-charts", "route":"first" } ], module2 : [ { "header":"Header2", "subheader":"Layanan dan Manfaat", "icon":"sap-icon://add-filter", "route":"first" }, { "header":"Header2", "subheader":"Layanan dan Manfaat", "icon":"sap-icon://add-filter", "route":"first" }, { "header":"Header2", "subheader":"Layanan dan Manfaat", "icon":"sap-icon://action", "route":"first" } ], module3 : [ { "header":"Header3", "subheader":"Investasi", "icon":"sap-icon://activate", "route":"first" }, { "header":"Header3", "subheader":"Investasi", "icon":"sap-icon://display-more", "route":"first" }, { "header":"Header3", "subheader":"Investasi", "icon":"sap-icon://customize", "route":"first" } ] }

inner_join