如果列包含字符串,则 R 过滤数据框

时间:2021-03-13 11:30:45

标签: r dataframe filter

注意事项:我对 R 的了解至少是巨大的。

我需要过滤一个数据框,只保留那些包含特定字符串的行。

到目前为止我尝试过的:

            DFfilters <- DFfilters  [which  (
                                                    grepl(DFfilters$EMPRESA             ,input$ExperienciasSearchHidden)
                                                |   grepl(DFfilters$PAIS                ,input$ExperienciasSearchHidden)
                                                |   grepl(DFfilters$NOMBREOT            ,input$ExperienciasSearchHidden)
                                                |   grepl(DFfilters$TIPOOBRA            ,input$ExperienciasSearchHidden)
                                                |   grepl(DFfilters$NOMBRECLI           ,input$ExperienciasSearchHidden)
                                                |   grepl(DFfilters$Anno_Exp            ,input$ExperienciasSearchHidden)
                                                |   grepl(DFfilters$pres_descrip_larga  ,input$ExperienciasSearchHidden)
                                            )
                                    ,]

输出:

空数据框,甚至丢弃我知道在 Anno_Exp 中包含 2008 的行。

此外,我收到此警告:

Warning in grepl(DFfilters$EMPRESA, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grepl(DFfilters$PAIS, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grepl(DFfilters$NOMBREOT, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grepl(DFfilters$TIPOOBRA, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grepl(DFfilters$NOMBRECLI, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grepl(DFfilters$Anno_Exp, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grepl(DFfilters$pres_descrip_larga, input$ExperienciasSearchHidden) :
  argument 'pattern' has length > 1 and only the first element will be used

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您在 grepl() 中交换了顺序,将输入放在首位:

grepl(input$ExperienciasSearchHidden,   DFfilters$EMPRESA),
# etc..

否则看起来还可以。真的不需要那里的 which