volfi
volfi

Reputation: 477

How to use multiple columns as inputs for apply function in R

I have the following dataframe,

df = data.frame("title1" = c("This is the Title", "This is a longer Title"), 
                "title2" = c("This is the Title", "This is an even longer Title"), 
                "url" = c("google.com","google.com"),
                "idx" = c(1,2),
                "insertion" = c("word1","word2")
                )

which looks like this:

enter image description here

What I would like to achieve is that for each row the word from column insertion gets inserted into the string in column title1 at the position of column idx. Here is my approach:

df$title1 <- sapply(df$title1, function(x) unlist(strsplit(x, " ")))
df$title1 <- sapply(df$title1, function(x) append(x, df$insertion, after = df$idx))
df$title1 <- sapply(df$title1, function(x) paste(x, collapse = ' '))

This however returns a warning and only uses row 1 of columns idx and insertion for each calculation of column title1. How do I get the desired behaviour?

Upvotes: 1

Views: 1352

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

A base R regex solution -

mapply(function(x, y, z) 
  sub(sprintf('((?:\\w+\\s){%d})(\\w+)', x), paste('\\1\\2', y), z), 
  df$idx - 1, df$insertion, df$title1)

#[1] "This word1 is the Title"      "This is word2 a longer Title"

We extract the idx - 1th word from each string and paste it with insertion word.

Upvotes: 1

volfi
volfi

Reputation: 477

A solution which works dynamically:

mapply(function(x, y, z)
  paste(append(unlist(strsplit(z, " ")), y, after = x), collapse = " "),
  df$idx, df$url , df$title1)

Thank you Ronak Shah for the hint!

Upvotes: 0

Related Questions