将R中的数据帧输出到.dly

时间:2016-08-29 23:09:54

标签: r csv dataframe

我正在为环境研究中心工作,他们的作物模拟使用的输入文件是“.dly”我从.csv中提取数据帧,我做了我需要的表格本身,但我需要导出这些数据帧为.dly格式。有谁知道怎么做?

1 个答案:

答案 0 :(得分:1)

这是一个可能是个好开头的黑客攻击函数:

#' Write fixed-width-format files.
#'
#' @param x data.frame
#' @param file character; if none provided, output to
#'   \code{\link{stdout}}
#' @param widths numeric vector of same length as columns in 'x'; if
#'   missing, will determine by finding the longest string within each
#'   column; strings that are over this width are silently trimmed
#' @param decimals integer, number of decimal places to preserve when
#'   printing the numbers (passed directly to \code{\link{round}}; if
#'   missing, no rounding is done
#' @param header logical, whether to include the column names at the
#'   top of the file (may have an effect on auto-determined column
#'   widths
#' @param justify character vector, indicates if each column should be
#'   justified 'l'eft or 'r'ight; may also be a single string with all
#'   columns, such as 'lrrlrll'
#' @param space integer, number of spaces to place between data,
#'   generally only useful if widths are auto-generated
#' @return nothing
#' @export
#' @examples
#' \dontrun{
#'
#' write.fwf(head(mtcars))
#' write.fwf(head(mtcars), space = 1)
#' write.fwf(head(mtcars), decimals = 1, space = 1)
#' write.fwf(head(mtcars), decimals = c(0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0), space = 1)
#' write.fwf(head(mtcars), widths = c(4,6,5,8,3,8,3,3,8,8,6))
#' write.fwf(head(mtcars), header = FALSE, space = 1)
#' write.fwf(head(mtcars), space = 1, justify = "lllllrrrrrr")
#'
#' }
write.fwf <- function(x, file, widths, decimals, header = TRUE,
                      justify = "r", space = 0L) {
  if (missing(file)) file <- stdout()
  if (length(justify) == 1L && nchar(justify) > 1L) {
      justify <- strsplit(justify, "")[[1]]
  }
  if (! length(justify) %in% c(1L, length(x))) {
    stop("justify must be of length 1 or same as the number of columns in 'x'")
  }
  # convert to character, with/without header
  xchar <- mapply(function(nm, vec, dec) {
    c(if (header) nm,
      if (is.numeric(vec)) sprintf(paste0("%0.", dec, "f"), vec) else as.character(a))
    }, names(x), x, if (missing(decimals)) "" else decimals, SIMPLIFY = FALSE)
  if (missing(widths)) widths <- sapply(xchar, function(col) max(nchar(col)))
  # enforce column widths
  jsign <- ifelse(justify == "l", "-", "")
  fmts <- mapply(paste0, "%", jsign, widths, "s", USE.NAMES = FALSE)
  y <- mapply(function(fmt, wid, cvec) substr(sprintf(fmt, cvec), 1L, wid),
              fmts, widths, xchar)
  # print it out
  spacechar <- paste(rep(" ", space), collapse = "")
  cat(paste(paste0(apply(y, 1, paste, collapse = spacechar)),
            collapse = "\n"), "\n",
      file = file)
  invisible()
}

使用示例:

write.fwf(head(mtcars))
# mpgcyldisp hpdratwtqsecvsamgearcarb
#  21  6 160110   4 3  16 0 1   4   4
#  21  6 160110   4 3  17 0 1   4   4
#  23  4 108 93   4 2  19 1 1   4   1
#  21  6 258110   3 3  19 1 0   3   1
#  19  8 360175   3 3  17 0 0   3   2
#  18  6 225105   3 3  20 1 0   3   1 

write.fwf(head(mtcars), space = 1)
# mpg cyl disp  hp drat wt qsec vs am gear carb
#  21   6  160 110    4  3   16  0  1    4    4
#  21   6  160 110    4  3   17  0  1    4    4
#  23   4  108  93    4  2   19  1  1    4    1
#  21   6  258 110    3  3   19  1  0    3    1
#  19   8  360 175    3  3   17  0  0    3    2
#  18   6  225 105    3  3   20  1  0    3    1 

write.fwf(head(mtcars), decimals = c(2,0,0,0,1,2,0,0,0,0,0), space = 1)
#   mpg cyl disp  hp drat   wt qsec vs am gear carb
# 21.00   6  160 110  3.9 2.62   16  0  1    4    4
# 21.00   6  160 110  3.9 2.88   17  0  1    4    4
# 22.80   4  108  93  3.8 2.32   19  1  1    4    1
# 21.40   6  258 110  3.1 3.21   19  1  0    3    1
# 18.70   8  360 175  3.1 3.44   17  0  0    3    2
# 18.10   6  225 105  2.8 3.46   20  1  0    3    1 

write.fwf(head(mtcars), widths = c(4,6,5,8,3,8,3,3,8,8,6))
#  mpg   cyl disp      hpdra      wtqse vs      am    gear  carb
#   21     6  160     110  4       3 16  0       1       4     4
#   21     6  160     110  4       3 17  0       1       4     4
#   23     4  108      93  4       2 19  1       1       4     1
#   21     6  258     110  3       3 19  1       0       3     1
#   19     8  360     175  3       3 17  0       0       3     2
#   18     6  225     105  3       3 20  1       0       3     1 

(注意由于列宽导致的名称丢失)