Oliver Humphreys
Oliver Humphreys

Reputation: 472

Using two columns in the sub function in R

I have a Data Table with two Text columns. I need to use column b to determine which letters to replace in column a with an "x".

I can do it using a for loop as in the code below. however my actual data set has 250,000+ rows so the script takes ages. Is there a more efficient way to do this? I considered lappy but couldn't get my head round it.

 DT <- data.table(a = c("ABCD","ABCD","ABCD","ABCD"), b = c("A","B","C", "D"))

DT$c <- ""

for (i in 1 : NROW(DT)){

  DT[i]$c <- sub(DT[i,b], "x", DT[i,a])

}

Upvotes: 0

Views: 80

Answers (1)

Bruno
Bruno

Reputation: 4151

Here is one approach using the tidyverse

library(tidyverse)

DT <- data.table::data.table(a = c("ABCD","ABCD","ABCD","ABCD"), b = c("A","B","C", "D"))

DT %>% 
  mutate(new_vec = str_replace_all(string = a,pattern = b,replacement = "X"))

Upvotes: 1

Related Questions