Return a different column of the dataframe if there is a grep match between two vectors

Viewed 98

I have a vector of file names and a dataframe that contains the "group" name for each of those filenames.

files <- c("data/backup/LATEST/20181514.X1235",
           "data/backup/LATEST/X1255+20181514",
           "data/backup/LATEST/20181514-X1237",
           "data/backup/LATEST/20181514-E1235",
           "data/backup/LATEST/20181514F1235",
           "data/backup/LATEST/M32_-X6635__20181514",
           "data/backup/LATEST/20181514-X1205",
           "data/backup/LATEST/l-A1230.20181514-XX")

groups <- data.frame(
                    ID = c("X1235","X1255","A1230","K93430",
                           "LOP0343","J3490","X1205","X6635",
                           "F1235","E1235","X1237"), 
                    Group = c("A","A","A",
                              "B","A","A",
                              "B","B","B",
                              "B","A")
)

As final result I want to have a dataframe with a column containing the full filepath from files and a second column showing its group.

How can I achieve this?

RESULT

                           filepath         group
1 data/backup/LATEST/20181514.X1235         A
2 data/backup/LATEST/X1255+20181514         A
3 data/backup/LATEST/20181514-X1237         A
4 data/backup/LATEST/20181514-E1235         B
5 data/backup/LATEST/20181514F1235          B
6 data/backup/LATEST/M32_-X6635__20181514   B
7 data/backup/LATEST/20181514-X1205         B
8 data/backup/LATEST/l-A1230.20181514-XX    A
3 Answers

Here is a way using stringr::str_detect

library(stringr)
strdet <- function(x){
      #browser()
      groups[str_detect(x,groups$ID),'Group']
      }

apply(df, 1, strdet)

[1] "A" "A" "A" "B" "B" "B" "B" "A"

PS:

  • I change files to a dataframe and
  • I assume you have one to one relation between files and group
  • I read the df using stringAsFactor=FALSE

    data

    df <- data.frame(files, stringsAsFactors = FALSE)
    
  • Using base R, you can create your group vector with:

    group_list <- lapply(groups$ID,
           function(patt) groups$Group[which(grepl(patt, files))])
    data.frame(files=files, group=unlist(group_list))
        files                                    group
        data/backup/LATEST/20181514.X1235        A
        data/backup/LATEST/X1255+20181514        A
        data/backup/LATEST/20181514-X1237        B
        data/backup/LATEST/20181514-E1235        B
        data/backup/LATEST/20181514F1235         A
        data/backup/LATEST/M32_-X6635__20181514  A
        data/backup/LATEST/20181514-X1205        B
        data/backup/LATEST/l-A1230.20181514-XX   A
    

    Is that what you were looking for?

    If you can assume the way the ID strings are built (one letter, four digits), with tidverse:

    data.frame(file=files) %>%
      mutate(ID=str_extract(file,"[A-Z]\\d{4}")) %>%
      left_join(groups,by="ID")
    

    I added stringsAsFactors=FALSE when creating groups to avoid a warning.

    And if you can't :

    library(fuzzyjoin)
    data.frame(file=files,stringsAsFactors=FALSE) %>%
      fuzzy_left_join(groups, by=list(x="file",y="ID"), match_fun=str_detect)
    
    Related