0

I can use write.table function to create an output data from a data.frame:

> write.table(head(cars), sep = "|", row.names=FALSE)
"speed"|"dist"
4|2
4|10
7|4
7|22
8|16
9|10

How can I create my own write.table function which creates an output like this (header with double pipes and data with preceding and succeeding pipes)?:

||"speed"||"dist"||
|4|2|
|4|10|
|7|4|
|7|22|
|8|16|
|9|10|
6
  • Have a look at the code of write.table. Do you want this function for writing to a file or displaying on screen? Commented Jan 8, 2014 at 12:30
  • I want it to write into a file, e.g. write.table(head(cars), sep = "|", file = "myfile.sep") Commented Jan 8, 2014 at 12:33
  • can you explain ||"speed"||"dist"|| (I understand the first double-pipe as there is an extra column of row numbers in the data but not in the header). But what's the function of the other two ||? So you have 6 separators in the header but only 4 in data? Otherwise, something like write.pipetable <- function(obj, file) write.table(obj, file, sep="|") is the likely answer. (Plus if you really need extra separators at the end and beginnig of the lines, you could add just empty columns to the left and right, and use na="" - Commented Jan 8, 2014 at 12:42
  • - and to have equal number of separators in the header and data, you could add row numbers as a variable in the data frame and use row.names=FALSE in write.table Commented Jan 8, 2014 at 12:44
  • Sorry, I edited the question Commented Jan 8, 2014 at 12:51

2 Answers 2

1

write.table can get you part of the way, but you will still need to do some fiddling around to get things to work just as you want.

Here's an example:

x <- capture.output(
  write.table(head(cars), sep = "|", row.names = FALSE, eol = "|\n"))
x2 <- paste0("|", x)
x2[1] <- gsub("|", "||", x2[1], fixed=TRUE)
cat(x2, sep = "\n")
# ||"speed"||"dist"||
# |4|2|
# |4|10|
# |7|4|
# |7|22|
# |8|16|
# |9|10|

As a function, I guess in its most basic form it could look something like:

write.myOut <- function(inDF, outputFile) {
  x <- capture.output(
    write.table(inDF, sep = "|", row.names = FALSE, eol = "|\n"))
  x <- paste0("|", x)
  x[1] <- gsub("|", "||", x[1], fixed=TRUE)
  cat(x, sep = "\n", file=outputFile)
}
Sign up to request clarification or add additional context in comments.

Comments

1

I don't think that it is possible with write.table. Here is a workaround:

# function for formatting a row
rowFun <- function(x, sep = "|") {
  paste0(sep, paste(x, collapse = sep), sep)
}

# create strings
rows <- apply(head(cars), 1, rowFun)
header <- rowFun(gsub("^|(.)$", "\\1\"", names(head(cars))), sep = "||")

# combine header and row strings
vec <- c(header, rows)

# write the vector
write(vec, sep = "\n", file = "myfile.sep")

The resulting file:

||"speed"||"dist"||
|4|2|
|4|10|
|7|4|
|7|22|
|8|16|
|9|10|

2 Comments

What do you want to happen if the header of data values contain a | character? Or if the header values contain a " character?
@RichieCotton Only the OP can answer this question. With the provided code, |s and "s will be preserved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.