I need to split the column into two columns.
Here is the input data set
df_in <- data.table(
product = c("Product 1 - 100g", "Product 2 - 150g", "Product 3", "Product 4 - keep it"),
tags = c(NA, NA, NA, NA))
The data after " - " should be extracted into the "tags" column. But not for all rows. Sometimes there are products with "-" we need to keep. I put these strings into separate list
list_of_keep_data <- c("keep it")
So, the output data set should be the following:
df_out <- data.table(
product = c("Product 1", "Product 2", "Product 3", "Product 4 - keep it"),
tags = c("100g", "150g", NA, NA))
Will be glad to see possible solutions by using "str_split", "str_location" or any other functions in a tidyverse style.
Thanks!