Create category based on keyword in R

2413 views r
5

I have a dataframe containing two columns: 1st column is the keyword and 2nd is the associated category.

keywords <- c("keyword1", "keyword2", "keyword3")
categories <- c("category1", "category2", "category3")

lookup_table <- data.frame(keywords, categories)

I would like that each time I have a new label, I check whether there is a category corresponding to it and if so, attach the category.

So for the following example below, there would be the value 'category1' attached to the first row in a new column:

new_labels <- c("keyword1 qefjhqek", "hfaef", "fihiz")

Help much appreciated!

answered question

1 Answer

13

Here just use str_extract to get the relevant text and join the reference table.

keywords <- c("keyword1", "keyword2", "keyword3")
categories <- c("category1", "category2", "category3")

lookup_table <- data.frame(keywords, categories)
new_labels <- c("keyword1 qefjhqek", "hfaef", "fihiz")

library(data.table)
library(tidyverse)
ref_tbl <- 
data.table(
    keywords = keywords
    ,categories = categories
)

as.data.table(new_labels) %>% 
    mutate(ref_key = str_extract(new_labels,'keyword[:digit:]')) %>% 
    left_join(
         ref_tbl
         ,by=c('ref_key'='keywords')
    )
#>          new_labels  ref_key categories
#> 1 keyword1 qefjhqek keyword1  category1
#> 2             hfaef     <NA>       <NA>
#> 3             fihiz     <NA>       <NA>

Created on 2018-11-09 by the reprex package (v0.2.1)

posted this

Have an answer?

JD

Please login first before posting an answer.