Practice: Data Manipulation in a Project

This exercise provides a short but complete code for the cycle of loading a dataset, saving, and reloading it in the R project environment that contains the folders "dataraw" and "dataderived".  This exercise does not count toward your grade. It is just for practice!


R code

Please execute this code on your computer to practice data manipulation in a project.

## dataraw ----
# Thompson SK. 2012. Sampling. 3rd ed. Wiley.
weather = read.table(file = "https://www.stat.sfu.ca/~thompson/dat...")
head(weather)
names(weather) = c("Day", "MaxTemp_C", "MinTemp_C", "MeanTemp_C",
                    "HDD", "CDD",
                    "Rain_mm", "Snow_cm", "Precip_mm")
write.csv(weather, file = "./dataraw/weather.csv", row.names = FALSE)

## dataderived ----
D = read.csv("./dataraw/weather.csv")
summary(D)
D$Snow_mm = D$Snow_cm * 10
summary(D)
saveRDS(D, file = "./dataderived/weather.rds")
saveRDS(D, file = paste0("./dataderived/weather_", Sys.Date(), ".rds"))
# W = readRDS("./dataderived/weather_2021-11-09.rds")

save.image(file = "./dataderived/image_weather.rdata")
# load("./dataderived/image_weather.rdata")


Source: Analytics Applied, https://www.youtube.com/watch?v=unQfJjhCZ1s
Creative Commons License This work is licensed under a Creative Commons Attribution 3.0 License.

Last modified: Monday, January 9, 2023, 3:52 PM