Skip to content

Instantly share code, notes, and snippets.

@dfalster
Created January 3, 2014 01:27
Show Gist options
  • Save dfalster/8230860 to your computer and use it in GitHub Desktop.
Save dfalster/8230860 to your computer and use it in GitHub Desktop.
Function to help write metadata for csv files. Assuming your headers are on line 1 of a file, this function takes the headers and transposes them, printing to a new file with suffix "metadata_" and with columns variable, units, description, notes
csv_metadata <- function(filename){
data <- read.csv(filename, stringsAsFactors=FALSE, fill=TRUE, header=TRUE, check.names = FALSE)
write.csv(data.frame(variable=names(data), units="",description="", notes=""), quote=TRUE, row.names=FALSE,
file = file.path(dirname(filename), paste0("metadata_",basename(filename))))
}
@dfalster
Copy link
Author

dfalster commented Jan 3, 2014

To apply to a series of files

for(f in list.files("data", full.names=TRUE))
    csv_metadata(f)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment