BlueDevilPride
BlueDevilPride

Reputation: 147

Variable substitution - R

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!

Upvotes: 0

Views: 1893

Answers (4)

akrun
akrun

Reputation: 887118

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))

Upvotes: 0

Jota
Jota

Reputation: 17611

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" 

Upvotes: 1

alistaire
alistaire

Reputation: 43334

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.

Upvotes: 2

Tad Dallas
Tad Dallas

Reputation: 1189

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"

Upvotes: 2

Related Questions