Home > OS >  Adding a column to the data that looks for a list of words and adds them if found
Adding a column to the data that looks for a list of words and adds them if found

Time:10-21

I have data that has a column with variable names and a column with the descriptions of variables:

library(data.table)
example_dat <- fread("var_nam description
      some_var this_is_som_var_kg
      other_var this_is_meters_for_another_var")
example_dat$description  <- gsub("_", " ", example_dat$description)

example_dat
     var_nam                    description
1:  some_var             this is som var kg
2: other_var this is meters for another var

I would like to create a separate column in this data which looks for certain units listed in a vector. I started out as follows:

vector_of_units <- c("kg", "meters")
example_dat <- setDT(example_dat)[, unit := ifelse(vector_of_units %in% description, vector_of_units, NA)]

But this gives

     var_nam                    description unit
1:  some_var             this is som var kg   NA
2: other_var this is meters for another var   NA

How should I write this syntax so that it gives the following output?

     var_nam                    description unit
1:  some_var             this is som var kg   kg
2: other_var this is meters for another var   meters

CodePudding user response:

Just change %in% with grepl and make some arrangement with paste,

setDT(example_dat)[, unit := ifelse(grepl(paste0(vector_of_units,collapse="|"), description), vector_of_units, NA)]

gives,

#         var_nam                    description   unit
#    1:  some_var             this is som var kg     kg
#    2: other_var this is meters for another var meters
  • Related