Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a new column using detecting the domain of a url from an existing column

I would like to create new column to the existing dataframe I have which will has as value for every row a specific url. This url exists in every row of the Content column of the following dataframe:

data <- read.table(text='"Content"     "date"     
  1     "a house a home https://example.com"     "12/31/2013"  
  2     "cabin ideas http://example.com/name in the woods"     "5/4/2013"  
  3     "motel is a hotel www.example.com"   "1/4/2013"', header=TRUE)

However the problem is that the url contains different version but the domain remain the same. The domain is this example.com. How is it possible to create the new column with urls using only the domain to detect it in every row?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

library(tidyverse)

data <- read.table(text = '"Content"     "date"
  1     "a house a home https://example.com"     "12/31/2013"
  2     "cabin ideas http://example.com/name in the woods"     "5/4/2013"
  3     "motel is a hotel www.example.com"   "1/4/2013"', header = TRUE)

data %>%
  mutate(url = Content %>% str_extract("(www\\.|http[s]?://)[A-z0-9./]*"))
#>                                            Content       date
#> 1               a house a home https://example.com 12/31/2013
#> 2 cabin ideas http://example.com/name in the woods   5/4/2013
#> 3                 motel is a hotel www.example.com   1/4/2013
#>                       url
#> 1     https://example.com
#> 2 http://example.com/name
#> 3         www.example.com

Created on 2022-03-30 by the reprex package (v2.0.0)

If this does not work you might expand your regex e.g. (?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading