将一列中的字符串替换为另一列

时间:2018-06-18 15:49:20

标签: r

我有一个包含两列的数据框,我希望用第二列替换第一列中的某些字符串。在下面的数据框中,我希望'em'标记被'a'标记替换为ulrs列上的url。

df1 = data.frame(text = c("I like <em'>Rstudio</em> very much", 
                      "<em'> Anaconda</em> is an amazing data science tool"), 
urls = c('https://www.rstudio.com/', 'https://anaconda.org/'))

我正在寻找下面的矢量。

text = c("I like <a href = 'https://www.rstudio.com/'>Rstudio</a> very much", 
        "<a href = 'https://anaconda.org/'> Anaconda</a> is an amazing data science tool")

1 个答案:

答案 0 :(得分:1)

使用gsubmapply的选项可以是:

mapply(function(x,y)gsub("<em'>.*</em>",x,y),df1$urls, df1$text)

# [1] "I like https://www.rstudio.com/ very much"            
# [2] "https://anaconda.org/ is an amazing data science tool"

数据:

df1 = data.frame(text = c("I like <em'>Rstudio</em> very much", 
                          "<em'> Anaconda</em> is an amazing data science tool"), 
                 urls = c('https://www.rstudio.com/', 'https://anaconda.org/'))