Home > Enterprise >  Modifying a column value conditionally in R
Modifying a column value conditionally in R

Time:07-16

I have a column that consists of IDs. I want to append <b>id</b> tag before and after the column.

I want to achieve something like:

id
-----------
<b>890G-</b>
<b>09K67</b>
<b>673N</b>
...

I could try something like:

lhs <- paste0('<b>')
rhs <- paste0('</b>')
gsub(lhs, rhs, df$id)

For some values, I get like this. But not the tag itself.

[1] "545-81"     "897582"     "f614-1"     "f6255"      "87967"     
[6] "584425"     "d239-7"     "a49089"     "03085-2"    "f9190"     
[11] "b58-0"      "b5446"      "875976"     "d5645"      "8086-4-" 

I could not find a way to apply all values of my column.

CodePudding user response:

Just build out the tag in a single call to paste0():

df$id <- paste0("<b>", df$id, "</b>")
  •  Tags:  
  • r
  • Related