code_learner93
code_learner93

Reputation: 631

How can I write a txt file with multiple delimiters [R]?

I'm trying to write a txt file that has multiple delimiters:

text|||value_1|value_2||||

Currently, I've tried:

write.table(variable, "file.txt", sep = "|", quote = FALSE, row.names = FALSE)

But I'm not sure how to use multiple delimiters given that sep only takes in 1 argument. Will appreciate any help! Thank you!

Upvotes: 0

Views: 204

Answers (1)

d.b
d.b

Reputation: 32558

Maybe you can write using one delimiter, read it back, and replace the one delimiter with whatever you want before writing again

write.table(mtcars, "tmp.txt", sep = "|", quote = FALSE)
d = readLines("tmp.txt")
d = gsub("|", "|||", d, fixed = TRUE)
writeLines(d, "tmp2.txt")

Upvotes: 1

Related Questions