0

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")
CHRD
  • 1,917
  • 1
  • 15
  • 38
  • That data.frame looks funny. Did you somehow store a list in that column instead of a character vector? Does this `df` have only one row or more than one? Is each row supposed to have more than one URL? It would be better to include sample data in a more [reproducible format](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (like a `dput()` for example). – MrFlick Apr 05 '17 at 20:56
  • Please see the edited question `@MrFlick`. – CHRD Apr 05 '17 at 21:11
  • Yeah, it's a bit unusual to see a list in a data.frame. In most cases you would expect a character vector. Maybe take another look at how you are building that data.frame. But if we `unlist()`, then I think `cat()` does what you need: `cat(unlist(df$url), file="url.txt")` – MrFlick Apr 05 '17 at 21:17
  • Thanks, I am very new to R so I'm most probably building my data.frame very badly. Although the answer of @fexjoo solved my problem for now, I'll definitely try your suggestion as well. Cheers :) – CHRD Apr 05 '17 at 21:21

1 Answers1

1

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")
Felix Grossmann
  • 1,224
  • 1
  • 11
  • 30