I have a vector:
vector_1 <- c('aa1/10', 'aa1/20', 'aa2/10')
And I have a data frame, with the column: product (some rows are empty)
product
hello123
hello123;aa1/20
World
I want to have another column, called: check. If one of the values in my vector_1 is in the column product, then I want to have a 1, else a 0.
I tried different things, but they didn't work out:
df$check <- ifelse(df$product %in% vector_1, 1,0)
Unfortunately, no results... So I tried:
df$check <- grepl(vector_1, df$product)
But there I received an warning message: In grep: argument pattern has lenght >1 and only the first element will be used.
How can I solve this?
Result:
product check
hello123 0
0
hello123;aa1/20 1
World 0
df$check <- as.numeric(grepl(pattern = paste0(vector_1, collapse = "|"), x = df$product))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.