`ifelse`语句根据匹配的字符串返回许多列

时间:2018-07-16 18:42:41

标签: r

我在earnings上有以下数据;

# A tibble: 6 x 24
  m_ticker ticker comp_name    comp_name_2       exchange currency_code per_end_date_fr0
  <chr>    <chr>  <chr>        <chr>             <chr>    <chr>         <date>          
1 AAPL     AAPL   APPLE INC    Apple Inc.        NSDQ     USD           2017-09-30      
2 AXP      AXP    AMER EXPRES~ American Express~ NYSE     USD           2017-12-31      
3 BA       BA     BOEING CO    The Boeing Compa~ NYSE     USD           2017-12-31      
4 CTR      CAT    CATERPILLAR~ Caterpillar Inc.  NYSE     USD           2017-12-31      
5 CSCO     CSCO   CISCO SYSTE~ Cisco Systems, I~ NSDQ     USD           2017-07-31      
6 SD       CVX    CHEVRON CORP Chevron Corporat~ NYSE     USD           2017-12-31      
# ... with 17 more variables: per_end_date_qr1 <date>, eps_mean_est_qr1 <dbl>,
#   street_mean_est_qr1 <dbl>, exp_rpt_date_qr1 <date>, exp_rpt_date_qr2 <date>,
#   exp_rpt_date_fr1 <date>, exp_rpt_date_fr2 <date>, late_last_flag <dbl>,
#   late_last_desc <chr>, source_flag <dbl>, source_desc <chr>, time_of_day_code <dbl>,
#   time_of_day_desc <chr>, per_end_date_qr0 <date>, eps_act_qr0 <dbl>,
#   per_end_date_qrm3 <date>, eps_act_qrm3 <dbl>

我还有一个ticker个符号的向量,称为tickers

tickers <- c("PYPL", "GOOG", "AAPL", "MSFT", "CSCO")

我正在尝试创建一个ifelse语句,该语句将从earnings数据中打印出以下几列的小表:

ticker | comp_name | exp_rpt_date_qr1 | exp_rpt_date_qr2 | time_of_day_desc 

因此,如果tickerearnings$ticker相匹配,则打印以上各列。

我尝试使用grepl打印报告警告消息的基本yes / no

ifelse(grepl(tickers, earnings$ticker), "yes", "no")

数据:

earnings <- structure(list(m_ticker = c("AAPL", "AXP", "BA", "CTR", "CSCO", 
    "SD", "DIS", "GE", "GS&", "HOMD", "IBM", "ITL", "JNJ", "CHL", 
    "KO", "MCD", "MMM", "MRK", "MSFT", "NIKE", "PFE", "PG", "SPM", 
    "UNIH", "UA", "VISA", "BEL", "WMS", "J"), ticker = c("AAPL", 
    "AXP", "BA", "CAT", "CSCO", "CVX", "DIS", "GE", "GS", "HD", "IBM", 
    "INTC", "JNJ", "JPM", "KO", "MCD", "MMM", "MRK", "MSFT", "NKE", 
    "PFE", "PG", "TRV", "UNH", "UTX", "V", "VZ", "WMT", "XOM"), comp_name = c("APPLE INC", 
    "AMER EXPRESS CO", "BOEING CO", "CATERPILLAR INC", "CISCO SYSTEMS", 
    "CHEVRON CORP", "DISNEY WALT", "GENL ELECTRIC", "GOLDMAN SACHS", 
    "HOME DEPOT", "INTL BUS MACH", "INTEL CORP", "JOHNSON & JOHNS", 
    "JPMORGAN CHASE", "COCA COLA CO", "MCDONALDS CORP", "3M CO", 
    "MERCK & CO INC", "MICROSOFT CORP", "NIKE INC-B", "PFIZER INC", 
    "PROCTER & GAMBL", "TRAVELERS COS", "UNITEDHEALTH GP", "UTD TECHS CORP", 
    "VISA INC-A", "VERIZON COMM", "WALMART INC", "EXXON MOBIL CRP"
    ), comp_name_2 = c("Apple Inc.", "American Express Company", 
    "The Boeing Company", "Caterpillar Inc.", "Cisco Systems, Inc.", 
    "Chevron Corporation", "The Walt Disney Company", "General Electric Company", 
    "The Goldman Sachs Group, Inc.", "The Home Depot, Inc.", "International Business Machines Corporation", 
    "Intel Corporation", "Johnson & Johnson", "JPMorgan Chase & Co.", 
    "Coca-Cola Company (The)", "McDonald's Corporation", "3M Company", 
    "Merck & Co., Inc.", "Microsoft Corporation", "NIKE, Inc.", "Pfizer Inc.", 
    "Procter & Gamble Company (The)", "The Travelers Companies, Inc.", 
    "UnitedHealth Group Incorporated", "United Technologies Corporation", 
    "Visa Inc.", "Verizon Communications Inc.", "Walmart Inc.", "Exxon Mobil Corporation"
    ), exchange = c("NSDQ", "NYSE", "NYSE", "NYSE", "NSDQ", "NYSE", 
    "NYSE", "NYSE", "NYSE", "NYSE", "NYSE", "NSDQ", "NYSE", "NYSE", 
    "NYSE", "NYSE", "NYSE", "NYSE", "NSDQ", "NYSE", "NYSE", "NYSE", 
    "NYSE", "NYSE", "NYSE", "NYSE", "NYSE", "NYSE", "NYSE"), currency_code = c("USD", 
    "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
    "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
    "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
    "USD"), per_end_date_fr0 = structure(c(17439, 17531, 17531, 17531, 
    17378, 17531, 17439, 17531, 17531, 17562, 17531, 17531, 17531, 
    17531, 17531, 17531, 17531, 17531, 17347, 17682, 17531, 17347, 
    17531, 17531, 17531, 17439, 17531, 17562, 17531), class = "Date"), 
        per_end_date_qr1 = structure(c(17712, 17712, 17712, 17712, 
        17743, 17712, 17712, 17712, 17712, 17743, 17712, 17712, 17712, 
        17804, 17712, 17712, 17712, 17712, 17712, 17774, 17712, 17712, 
        17712, 17712, 17712, 17712, 17712, 17743, 17712), class = "Date"), 
        eps_mean_est_qr1 = c(2.19, 1.83, 3.43, 2.66, 0.63, 2.1, 2.04, 
        0.18, 4.67, 2.85, 3.03, 0.99, 2.06, 2.27, 0.6, 1.93, 2.59, 
        1.03, 1.07, 0.61, 0.75, 0.91, 2.44, 3.03, 1.86, 1.09, 1.15, 
        1.21, 1.24), street_mean_est_qr1 = c(2.187, 1.83, 3.434, 
        2.658, 0.689, 2.098, 2.043, 0.178, 4.674, 2.847, 3.031, 0.99, 
        2.055, 2.27, 0.601, 1.929, 2.594, 1.03, 1.074, 0.609, 0.748, 
        0.906, 2.436, 3.031, 1.858, 1.089, 1.145, 1.212, 1.244), 
        exp_rpt_date_qr1 = structure(c(17743, 17730, 17737, 17742, 
        17758, 17739, 17750, 17732, 17729, 17764, 17730, 17738, 17729, 
        17815, 17737, 17738, 17736, 17739, 17731, 17799, 17743, 17743, 
        17731, 17729, 17736, 17737, 17736, 17759, 17739), class = "Date"), 
        exp_rpt_date_qr2 = structure(c(17836, 17821, 17828, 17827, 
        17856, 17830, 17843, 17823, 17820, 17848, 17820, 17829, 17820, 
        17907, 17828, 17827, 17827, 17830, 17829, 17885, 17834, 17823, 
        17822, 17820, 17827, 17828, 17822, 17850, 17830), class = "Date"), 
        exp_rpt_date_fr1 = structure(c(17836, 17913, 17926, 17920, 
        17758, 17928, 17843, 17919, 17912, 17946, 17913, 17920, 17918, 
        17907, 17942, 17925, 17920, 17928, 17731, 18074, 17925, 17743, 
        17918, 17911, 17919, 17828, 17918, 17946, 17928), class = "Date"), 
        exp_rpt_date_fr2 = structure(c(NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_), class = "Date"), late_last_flag = c(0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0), late_last_desc = c("Not late", 
        "Not late", "Not late", "Not late", "Not late", "Not late", 
        "Not late", "Not late", "Not late", "Not late", "Not late", 
        "Not late", "Not late", "Not late", "Not late", "Not late", 
        "Not late", "Not late", "Not late", "Not late", "Not late", 
        "Not late", "Not late", "Not late", "Not late", "Not late", 
        "Not late", "Not late", "Not late"), source_flag = c(1, 1, 
        1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 
        1, 1, 1, 1, 1, 1, 2, 1), source_desc = c("Company", "Company", 
        "Company", "Company", "Estimated", "Company", "Company", 
        "Company", "Company", "Estimated", "Company", "Company", 
        "Company", "Estimated", "Company", "Company", "Company", 
        "Company", "Company", "Estimated", "Company", "Company", 
        "Company", "Company", "Company", "Company", "Company", "Estimated", 
        "Company"), time_of_day_code = c(1, 1, 2, 2, 4, 2, 1, 2, 
        2, 4, 1, 1, 2, 4, 2, 2, 2, 2, 1, 4, 2, 2, 2, 2, 2, 1, 2, 
        4, 2), time_of_day_desc = c("After market close", "After market close", 
        "Before the open", "Before the open", "Unknown", "Before the open", 
        "After market close", "Before the open", "Before the open", 
        "Unknown", "After market close", "After market close", "Before the open", 
        "Unknown", "Before the open", "Before the open", "Before the open", 
        "Before the open", "After market close", "Unknown", "Before the open", 
        "Before the open", "Before the open", "Before the open", 
        "Before the open", "After market close", "Before the open", 
        "Unknown", "Before the open"), per_end_date_qr0 = structure(c(17621, 
        17621, 17621, 17621, 17651, 17621, 17621, 17621, 17621, 17651, 
        17621, 17621, 17621, 17712, 17621, 17621, 17621, 17621, 17621, 
        17682, 17621, 17621, 17621, 17621, 17621, 17621, 17621, 17651, 
        17621), class = "Date"), eps_act_qr0 = c(2.73, 1.86, 3.64, 
        2.82, 0.6, 1.9, 1.84, 0.16, 6.95, 2.08, 2.45, 0.87, 2.06, 
        2.29, 0.47, 1.79, 2.5, 1.05, 0.95, 0.69, 0.77, 1, 2.46, 3.04, 
        1.77, 1.11, 1.17, 1.14, 1.09), per_end_date_qrm3 = structure(c(17347, 
        17347, 17347, 17347, 17378, 17347, 17347, 17347, 17347, 17378, 
        17347, 17347, 17347, 17439, 17347, 17347, 17347, 17347, 17347, 
        17409, 17347, 17347, 17347, 17347, 17347, 17347, 17347, 17378, 
        17347), class = "Date"), eps_act_qrm3 = c(1.67, 1.47, 2.55, 
        1.49, 0.55, 0.91, 1.58, 0.28, 3.95, 2.25, 2.97, 0.72, 1.83, 
        1.76, 0.59, 1.73, 2.58, 1.01, 0.98, 0.57, 0.67, 0.85, 1.92, 
        2.46, 1.85, 0.86, 0.96, 1.08, 0.78)), .Names = c("m_ticker", 
    "ticker", "comp_name", "comp_name_2", "exchange", "currency_code", 
    "per_end_date_fr0", "per_end_date_qr1", "eps_mean_est_qr1", "street_mean_est_qr1", 
    "exp_rpt_date_qr1", "exp_rpt_date_qr2", "exp_rpt_date_fr1", "exp_rpt_date_fr2", 
    "late_last_flag", "late_last_desc", "source_flag", "source_desc", 
    "time_of_day_code", "time_of_day_desc", "per_end_date_qr0", "eps_act_qr0", 
    "per_end_date_qrm3", "eps_act_qrm3"), row.names = c(NA, -29L), class = c("tbl_df", 
    "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:3)

您可以使用grepl代替%in%。 此外,如果您要做的只是选择要打印的特定行和列,则可以使用subset

> keepcols = c('ticker','comp_name','exp_rpt_date_qr1','exp_rpt_date_qr2','time_of_day_desc')
> subset(earnings, ticker %in% tickers, select = keepcols)
   ticker      comp_name exp_rpt_date_qr1 exp_rpt_date_qr2   time_of_day_desc
1    AAPL      APPLE INC       2018-07-31       2018-11-01 After market close
5    CSCO  CISCO SYSTEMS       2018-08-15       2018-11-21            Unknown
19   MSFT MICROSOFT CORP       2018-07-19       2018-10-25 After market close