Embed hyperlink into a column with HTML in a dataset

Viewed 112

I have a dataset that looks basically like this. (Not real links)

      Name     Link
1.    cat      https://cat.co.uk...
2.    dog      https://dog.co.uk...
3.    horse    https://horse.co.uk...
4.    snek     https://snake.co.uk...

I would like the names to have their respective link connected to them using HTML. So that I can embed a table on my website with clickable links. Something that would look like this:

      Name with link                                Link
1.    <a href="https://cat.co.uk..."> cat </a>       https://cat.co.uk...
2.    <a href="https://dog.co.uk..."> dog </a>       https://dog.co.uk...
3.    <a href="https://horse.co.uk..."> horse </a>   https://horse.co.uk...
4.    <a href="https://snake.co.uk..."> snek </a>    https://snake.co.uk...

What would be the best way to do this either in R or Python, or maybe simply Excel?

1 Answers

In you simply can paste the desired characters to your dataframe:

df2 <- data.frame(`Name with Link` = paste0('<a href="', df1$Link, '"> ', df1$Name, ' </a>'),
                   Link = df1$Link)

#>                                 Name.with.Link                   Link
#> 1     <a href="https://cat.co.uk..."> cat </a>   https://cat.co.uk...
#> 2     <a href="https://dog.co.uk..."> dog </a>   https://dog.co.uk...
#> 3 <a href="https://horse.co.uk..."> horse </a> https://horse.co.uk...
#> 4  <a href="https://snake.co.uk..."> snek </a> https://snake.co.uk...
## input data
df1 <- structure(list(Name = c("cat", "dog", "horse", "snek"), 
                      Link = c("https://cat.co.uk...", 
                                "https://dog.co.uk...", 
                                "https://horse.co.uk...", 
                                "https://snake.co.uk...")), 
                 row.names = c(NA, -4L), class = "data.frame")


In you can paste characters using &; assuming that your data (with headers) starts at cell A1, and you want your new column at C, see this example for the first row:

="<a href=""" & B2 & """> " & A2 & " </a>"

You can expand this for the other rows.

Related