Data curation

Exporting and importing data

First create a tiny data set in Excel:

# comments at the top
# beaucoup metadata
ID, Treatment, Biomass, Notes
1, Control, 30.3, 
2, HighN, 13.0, 
3, HighN, NA, broken scale
4, Control, 35.3,

Use read.table to bring in data

my_data <- read.table(file="path/to/data.csv",
                    header=TRUE,
                    sep=",",
                    comment.char="#")

# inspect object
str(my_data)

# now add a column
my_data$newVar <- runif(4)
head(my_data)

Use write.table to export to a data file

write.table(x=my_data,
            file="Path/To/OutputFileName.csv",
            HEADER=TRUE,
            sep=",")

But this is not a good way to save or share data objects if we are working in R. Some researchers use the save() function, which preserves the whole environment, but once it is restored with load(), the variable names cannot be changed. It is better to use `saveRDS().

saveRDS(): useful when you are working only in R

saveRDS(my_data, file="Path/To/FileName.RDS") # .RDS suffix is not required, but good for clarity

This only saves a single R object as a binary, but remember, you can bundle up many things into a single list!

Use readRDS() to restore it.

readRDS()

data_in <-readRDS("FileName.RDS")