J001
J001

Reputation: 15

How can I apply a combination of if_else and grepl function to selected columns in R?

after searching for 2 days, I would appreciate some help.

I have a data frame that represents almost 20,000 users (rows) and names of Twitter lists they've been added to (column 5:197). Please see the picture to get an idea of my data frame.

enter image description here

My aim is to recode the table, or more precisely columns 5 to 197. If the list names contain certain keywords, I'd like to overwrite the text with 1, if not with 0.

I'm able to do it column by column with the following code combining if_else and grepl:

fashion_lists$X1 <- if_else(grepl("fashion|cloth|apparel|textile|material|garment|wardrobe|shoes|sneakers|footwear|sportswear|streetwear|
                              menswear|athleisure|hautecouture|hypebeast", fashion_lists$X1) & 
                        !grepl("rev|clean|vegan|warrior|sdg|capsule|worker|whomademyclothes|conscious|circular|slow|responsible|smart|
                               secondhand|sust|eco|organic|green|ethical|fair|environment|repurposed|upcycl|recycl|reus", fashion_lists$X1), 
                        1, 0)

This code provides me with this result which is what I'm looking for (see X1):

enter image description here

How can I do it for all columns without copy/pasting my code 193 times? I tried to combine the above in an apply function but nothing has worked so far.

Thanks so much for your help!

Upvotes: 0

Views: 161

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520988

We can try using lapply syntax targeting only the 5th through 197th columns. Note that I define a helper function below, and I avoid using ifelse, since the boolean result can simply be cast to 1 or 0 to get the behavior you want.

func <- function(x) {
    as.numeric(grepl("fashion|cloth|apparel|textile|material|garment|wardrobe|shoes|sneakers|footwear|sportswear|streetwear|menswear|athleisure|hautecouture|hypebeast", x) &
               !grepl("rev|clean|vegan|warrior|sdg|capsule|worker|whomademyclothes|conscious|circular|slow|responsible|smart|secondhand|sust|eco|organic|green|ethical|fair|environment|repurposed|upcycl|recycl|reus", x))
}
cols <- names(fashion_lists)[5:197]
fashion_lists[cols] <- lapply(fashion_lists[cols], func)

Upvotes: 1

Related Questions