代码之家  ›  专栏  ›  技术社区  ›  NinjaR

筛选列值以一组模式结尾的行

  •  -1
  • NinjaR  · 技术社区  · 8 年前

    File_name     Folder
    ord.cpp        1
    rod.ibo        1
    ppol.h         2
    lko.cpp        3
    rto.cp         3
    tax.mo         2
    t_po..lo.cpp   4
    

    4 回复  |  直到 8 年前
        1
  •  1
  •   Tim Biegeleisen    8 年前

    使用 grepl

    df_subset <- df[grepl("\\.(?:cpp|h)$", df$File_name), ]
    df_subset
    
         File_name Folder
    1      ord.cpp      1
    3       ppol.h      2
    4      lko.cpp      3
    7 t_po..lo.cpp      4
    

    Demo

        2
  •  2
  •   tmfmnk    7 年前

    dplyr stringr 解决方案:

    df %>%
     filter(str_detect(File_name, ".cpp|.h"))
    
         File_name Folder
    1      ord.cpp      1
    2       ppol.h      2
    3      lko.cpp      3
    4 t_po..lo.cpp      4
    

    或者只是 :

    df %>%
     filter(grepl(".cpp|.h", File_name))
    
         File_name Folder
    1      ord.cpp      1
    2       ppol.h      2
    3      lko.cpp      3
    4 t_po..lo.cpp      4
    
        3
  •  0
  •   Ronak Shah    8 年前

    file_ext 函数来自 tools

    library(tools)
    df[file_ext(df$File_name) %in% c("cpp", "h"), ]
    
    #     File_name Folder
    #1      ord.cpp      1
    #3       ppol.h      2
    #4      lko.cpp      3
    #7 t_po..lo.cpp      4
    
        4
  •  0
  •   Saurabh Chauhan    8 年前

    碱性溶液:

    # Looking for a string eding with .cpp or .h
    df[endsWith(df$File_name,(".cpp"))|endsWith(df$File_name,(".h")),]
    

    输出:

         File_name Folder
    1      ord.cpp      1
    3       ppol.h      2
    4      lko.cpp      3
    7 t_po..lo.cpp      4