Filipe Rigueiro
Filipe Rigueiro

Reputation: 57

For loop and gsub R

I have a question about having a gsub in a for loop in R.

I have a dataframe (catalog) with "sku" and "cat" columns, they are a sku ID and a Catalog ID for the same product from different sources.

I then have a dataframe (image_data) with sku, and image descriptions (image_data).

I want to create a new column(new_image_description) where all instances of sku's is replaced by the corresponding catalog number (see bellow) from the column image_des.

But only replaces some and other not. bellow is some dummy data.

    catalog <- data.frame(sku = c('AX1', "BX2", "CX2", "DXX"), 
    cat = c("AL1", "AL2", "AL3", "AL4"))


    image_data <- data.frame(sku = c("CX2", "AX1", "BX2"), 
image_des = c("CX2 is a good product", "AX1 is not bad",  "BX2 is great as well as DXX"))

    image_data$new_image_description <- NA

    for (i in 1:nrow(catalog)) {

      image_data$new_image_description <-  gsub(as.character(catalog$sku[i]), as.character(catalog$cat[i]), image_data$image_des)

    }

I would appreciate any input that can explain why it does not substitute the sku.

If I do it individually it works.

Best to all

Upvotes: 1

Views: 502

Answers (2)

Roman
Roman

Reputation: 4999

Solution without loading any libraries. You need a nested loop.

  sku                   image_des                     new_des
1 CX2       CX2 is a good product       AL3 is a good product
2 AX1              AX1 is not bad              AL1 is not bad
3 BX2 BX2 is great as well as DXX AL2 is great as well as AL4

Code

for (i in 1:nrow(image_data)) {
    for (j in 1:nrow(catalog)) {
        image_data$new_des[i] <-  gsub(catalog$sku[j],
                                       catalog$cat[j],
                                       image_data$new_des[i])
    }
}

Data

image_data <- data.frame(sku = c("CX2", "AX1", "BX2"),
                         image_des = c("CX2 is a good product",
                                       "AX1 is not bad", 
                                       "BX2 is great as well as DXX"),
                         stringsAsFactors = FALSE)
image_data$new_des <- image_data$image_des

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627536

You may use qdap::mgsub to perform multiple search and replace operations:

Multiple Gsub
A wrapper for gsub that takes a vector of search terms and a vector or single value of replacements.

See the R demo:

> library(qdap)
> image_data$new_image_description <- mgsub(as.character(catalog$sku), as.character(catalog$cat), image_data$image_des)
> image_data
  sku                   image_des       new_image_description
1 CX2       CX2 is a good product       AL3 is a good product
2 AX1              AX1 is not bad              AL1 is not bad
3 BX2 BX2 is great as well as DXX AL2 is great as well as AL4

Upvotes: 4

Related Questions