I want to categorize products in a basket using dictionaries with all the product sorts of a category. I want to check in particular if a product from my basket is an element of my dictionary, and if this is true I would like to replace the product with the name of the category (dictionary).
I imported my dictionary in a list in csv and tried to compare the products of my basket with %in% but this didn't work.
BD19jfiles[[i]]$product$name return the products of a basket. Pistolets is a list with chars (the dictionary).
l <- length(BD19jfiles)
for(i in 1:l) {
if(BD19jfiles[[i]]$product$name %in% Pistolets){
grepl('Pistolet', BD19jfiles[[i]]$product$name)
}
}
I would like to replace the products who return true for 'BD19jfiles[[i]]$product$name %in% Pistolets' with the word 'Pistolet'
We can use lapply
to loop over the list
, and update the 'name' element by checking for 'PISTOLET' substring in that element with grepl
and assign those to "Pistolet"
lapply(BD19jfiles, function(x) {
x$product$name[grepl("PISTOLET", x$product$name)] <- "Pistolet"
x
})
If the dictionary identifier is Pistolets
lapply(BD19jfiles, function(x) {
x$product$name[x$product$name %in% Pistolets] <- "Pistolet"
x
})
If it is a substring, use grepl
nm1 <- paste0("\\b(", paste(unlist(Pistolets), collapse="|"), ")\\b")
out <- lapply(BD19jfiles, function(x) {
x$product$name[grepl(nm1, x$product$name)] <- "Pistolet"
x
})
lapply(BD19jfiles, function(x){
x$product$name=gsub(paste(Pistolets,collapse="|"),"Pistolets",x$product$name)
x
}
Here you are changing words that match any of the words in your dictionary to Pistolet
.
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.