Skip to content

Instantly share code, notes, and snippets.

View oousmane's full-sized avatar
🎯
Focusing

Ousmane Ouedraogo oousmane

🎯
Focusing
View GitHub Profile
@haozhu233
haozhu233 / write_fwf.R
Last active May 17, 2024 09:41
Generate fixed width file in R
#' Generate fixed width file in R
#' @description This simple function creates fixed width file with no
#' extra dependencies.
#' @param justify "l", "r" or something like "lrl" for left, right, left.
#' @examples dt <- data.frame(a = 1:3, b = NA, c = c('a', 'b', 'c'))
#' write_fwf(dt, "test.txt", width = c(4, 4, 3))
#' @export
write_fwf = function(dt, file, width,
justify = "l", replace_na = "NA") {
fct_col = which(sapply(dt, is.factor))