Home > Software design >  How use colon as a field separator in dataframe?
How use colon as a field separator in dataframe?

Time:10-30

I need to use colons as field separator when printing a dataframe. I thought it was possible to do this using the print function, but it turned out not to be the case.

CodePudding user response:

You would need to write your own printing function. For instance,

library(tidyr)

print_delim <- function(x, sep = ":") {
  stopifnot(is.data.frame(x))
  xprint <- unite(x, col = col, everything(), sep = sep)
  cat(
    c(paste0(names(x), collapse = sep), 
      xprint$col), 
    sep = "\n"
  )
  invisible(x)
}

df1 <- data.frame(
  var1 = LETTERS[1:5],
  var2 = 1:5,
  var3 = -5:-1,
  var4 = letters[22:26]
)

print_delim(df1)
var1:var2:var3:var4
A:1:-5:v
B:2:-4:w
C:3:-3:x
D:4:-2:y
E:5:-1:z

CodePudding user response:

You should simply retry i think that's the best option IT should be there

  • Related