选择与任意列中的字符向量匹配的行

问题描述 投票:0回答:1

我想提取与任意列中的字符向量 (

tax_table
) 匹配的 df (
gross
) 行。我知道指定特定列是:

grossasv <- tax_table[tax_table$column %in% gross,]

但是每列怎么样?

r vector character row
1个回答
0
投票

有两种方法:

library(tidyverse)

#1
filter(aux_df, if_any(everything(),\(x) x %in% aux_tz))

#2
aux_df[list_c(map(aux_df, \(x) which(x %in% aux_tz))), ]

相同的输出:

# A tibble: 4 × 3
  col_1           col_2                      col_3            
  <chr>           <chr>                      <chr>            
1 Africa/Cairo    America/Costa_Rica         America/Boa_Vista
2 Africa/Sao_Tome Africa/Banjul              America/Belize   
3 Africa/Kinshasa America/Caracas            Africa/Bamako    
4 Africa/Timbuktu America/Argentina/San_Juan Africa/Banjul 

玩具数据:

# Toy data
set.seed(123)
aux_df <- tibble(
  col_1 = sample(OlsonNames()[1:100], 10),
  col_2 = sample(OlsonNames()[1:100], 10),
  col_3 = sample(OlsonNames()[1:100], 10))

aux_tz <- sample(OlsonNames()[1:100], 10) 

> aux_tz
 [1] "Africa/Lagos"                   "Africa/Bamako"                  "Africa/Banjul"                 
 [4] "Africa/Mbabane"                 "America/Asuncion"               "Africa/Freetown"               
 [7] "Africa/Juba"                    "America/Argentina/Buenos_Aires" "Africa/Tunis"                  
[10] "America/Costa_Rica" 
     
> aux_df
# A tibble: 10 × 3
   col_1                          col_2                      col_3                 
   <chr>                          <chr>                      <chr>                 
 1 Africa/Kinshasa                America/Caracas            Africa/Bamako         
 2 America/Barbados               America/Catamarca          Africa/Mogadishu      
 3 Africa/Timbuktu                America/Argentina/San_Juan Africa/Banjul         
 4 Africa/Cairo                   America/Costa_Rica         America/Boa_Vista     
 5 America/Argentina/Rio_Gallegos America/Anguilla           Africa/Lubumbashi     
 6 Africa/Mogadishu               America/Cayenne            America/Bahia_Banderas
 7 Africa/Sao_Tome                Africa/Banjul              America/Belize        
 8 Africa/Monrovia                America/Cayman             Africa/Monrovia       
 9 America/Coral_Harbour          America/Argentina/Ushuaia  America/Atka          
10 Africa/Harare                  Africa/Johannesburg        Africa/Casablanca
© www.soinside.com 2019 - 2024. All rights reserved.