Search code examples
rurldataframewrite.table

Writing a .txt with URLs in R


I have a data frame with one variable url and each observation is a url. It looks like this:

> df$url

[[1]]
[1] "https://url1"

[[2]]
[1] "https://url2"

What I want to do is to output a .txt file called url.txt where each url is pasted in a straight line separated only by a space (no line breaks, quotation marks etc.). Like this:

https://url1 https://url2

I have tried write.table, however the output look strange. For example every : and / in the urls are replaced by a .. Any ideas on how to achieve this? Thanks :)

Edit

> dput(df)
structure(list(url = list("https://url1", 
  "https://url2")), .Names = "url", row.names = c(NA,
-2L), class = "data.frame")

Solution

  • I am a bit confused about the structure of your "dataframe". Assuming that your final data is truly a dataframe, this is my solution:

    library(readr)
    
    # create your example dataset
    df <- data.frame(url = c("https://a.b.c/d1-e/2f3g", "https://h.i.j/k4-l/5m5n"))
    
    # Concatenate URLs
    string <- paste(df$url, collapse=" ")
    
    # Write file
    write_file(string, "url.txt")