Home > Mobile >  How to remvoe comma from one row (first row) in write_delim?
How to remvoe comma from one row (first row) in write_delim?

Time:12-22

How can I not write , after the date ( first row) in the following example data.

library(tidyverse)
dt <- structure(list(
  tmax = c(1990101, 5.974, 6.15, 5.806, 5.694),
  tmin = c(NA, 0.5, 0.475, -0.083, 0.944)
), row.names = c(NA,
                 -5L), class = c("tbl_df", "tbl", "data.frame"))
write_delim(
  dt,
  file = "test_comma.txt",
  delim = ",",
  append = F,
  na = "",
  col_names = F
)

How to remove the comma after date when saved as a text file. I want like in left on the image below.enter image description here

CodePudding user response:

By using append and two separate write operations this can be done with using sed (not the first blank line in the file in your example I'm not sure if that is on intend otherwise adjust the write_lines statement):

library(tidyverse)
dt <- structure(list(
  tmax = c(1990101, 5.974, 6.15, 5.806, 5.694),
  tmin = c(NA, 0.5, 0.475, -0.083, 0.944)
), row.names = c(NA,
                 -5L), class = c("tbl_df", "tbl", "data.frame"))
write_lines(c('',dt[1,1]),file = "test_comma.txt")
write_delim(
  dt[-1,],
  file = "test_comma.txt",
  delim = ",",
  append = T,
  na = "",
  col_names = F
)
  • Related