R/add_col_widths.R

Defines functions add_col_widths

Documented in add_col_widths

#' Adds width as a attribute to each column from a list of tibbles
#'
#' @export
#'
#' @keywords internal
#'
#' @param tbl_list a list of tibbles
#' @param fwf_pos a list of returns from \code{fwf_position()} with
#' length that matches length of \code{tbl_list}
#'
#' @importFrom dplyr mutate_at
#' @importFrom purrr map
#'
#' @return a tibble containing the data from the raw DSSAT file
#'
add_col_widths <- function(tbl_list,fwf_pos){
  tbl_list <- map(1:length(tbl_list),function(i){
    tbl <- tbl_list[[i]]
    widths <- fwf_pos[[i]]$end - fwf_pos[[i]]$begin
    cnames <- fwf_pos[[i]]$col_names
    for(cl in 1:length(cnames)){
      tbl <- mutate_at(tbl,cnames[cl],~structure(.,width=widths[cl]))
    }
    return(tbl)
  })
  return(tbl_list)
}

Try the DSSAT package in your browser

Any scripts or data that you put into this service are public.

DSSAT documentation built on Nov. 9, 2023, 1:08 a.m.