0

Very simple question, but am struggling to find the solution.

Want to go from this:

a = c("the sky is color", "the color dog", "grass is color") 
b = c("blue","brown","green") 
df = data.frame(a,b) 

To this:

a = c("the sky is color", "the color dog", "grass is color") 
b = c("blue","brown","green") 
c = c("the sky is blue", "the brown dog", "grass is green")
df = data.frame(a,b,c) 

Tried using gsub:

df$d <- gsub('color', df$b, df$a)

But received this error message:

argument 'replacement' has length > 1 and only the first element will be used 

Will the solution also work for integers? Thank you!

BlueDevilPride
  • 147
  • 5
  • 13
  • Have you seen this? http://stackoverflow.com/questions/19424709/r-gsub-pattern-vector-and-replacement-vector – joemienko Jan 31 '17 at 02:49

4 Answers4

2

I imagine there's a neat vectorized solution, but you can do it with a simple apply statement.

a = c("the sky is color", "the color dog", "grass is color") 
b = c("blue","brown","green") 
df = data.frame(a,b) 

df$d <- apply(df,1,function(x){gsub('color', x[2], x[1])})


df$d

[1] "the sky is blue" "the brown dog" "grass is green"

Tad Dallas
  • 1,179
  • 5
  • 13
  • 1
    You could do something like `\`regmatches<-\`(df$a, gregexpr("color", df$a), as.list(df$b), invert=FALSE )` but the loop via `apply` is actually quicker. – thelatemail Jan 31 '17 at 03:07
2

Most functions from the stringi package are vectorized, so you can just do

df$c <- stringi::stri_replace_all_fixed(df$a, 'color', df$b)

df
##                  a     b               c
## 1 the sky is color  blue the sky is blue
## 2    the color dog brown   the brown dog
## 3   grass is color green  grass is green

Implement in dplyr or data.table if you prefer.

alistaire
  • 42,459
  • 4
  • 77
  • 117
0

We can use mapply

df$a <- mapply(sub, 'color', df$b, df$a)
df$a
#[1] "the sky is blue" "the brown dog"   "grass is green" 

Or we can use str_replace_all from stringr

library(stringr)
df$a <- str_replace_all(df$a, 'color', df$b)
df$a
#[1] "the sky is blue" "the brown dog"   "grass is green" 

Or using the tidyverse

library(dplyr)
df %>%
    mutate(a = str_replace_all(a, 'color', b))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

There is the Vectorize function that lets you specify some arguments to vectorize. In this case, you want to vectorize the "replacement" argument and the string argument named "x":

(df$c <- Vectorize(gsub, c("replacement", "x"))("color", df$b, df$a, fixed = TRUE))
# [1] "the sky is blue" "the brown dog"   "grass is green" 
Jota
  • 17,281
  • 7
  • 63
  • 93