Skip to content

Instantly share code, notes, and snippets.

@Jianghao
Last active March 4, 2016 06:45
Show Gist options
  • Save Jianghao/f02a8649d45ff22fd5ba to your computer and use it in GitHub Desktop.
Save Jianghao/f02a8649d45ff22fd5ba to your computer and use it in GitHub Desktop.
#' Get the detail of ncdf4 file variable as a data.frame
#'
#' @param nc opened nc4 files
#' @return data.frame of variable information
#' @keywords nc attibute
#' @export
#' @examples
#' ncatt_get_df(nc)
ncatt_get_df <- function(nc, items=c("long_name", "units")){
options(stringsAsFactors=FALSE)
var.names <- as.vector(names(nc$var))
var.list <- lapply(var.names, function(var) ncatt_get(nc, var))
var.list.items <- lapply(1:length(var.list), function(i) var.list[[i]][items])
var.df <- do.call("rbind", lapply(var.list.items, data.frame))
# var.df <- plyr::ldply(var.list, data.frame) # altertive is using ldply
var.df <- cbind(var=var.names, var.df)
return(var.df)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment