X-Git-Url: https://code.communitydata.science/stats_class_2020.git/blobdiff_plain/3f31579d7ac0cbade8c9fc684ea0130682160cd5..56abef0030cf6ac98f97bf3e1f057c402bf698e4:/r_tutorials/w04-R_tutorial.rmd?ds=inline diff --git a/r_tutorials/w04-R_tutorial.rmd b/r_tutorials/w04-R_tutorial.rmd index c5e6c64..e5005f6 100644 --- a/r_tutorials/w04-R_tutorial.rmd +++ b/r_tutorials/w04-R_tutorial.rmd @@ -54,9 +54,21 @@ head(more.cars) table(more.cars == my.mtcars) ``` -When find yourself trying to load a tabular data file that consists of plain text, but has some idiosyncratic difference from a csv (e.g., it is tab-separated instead of comma-separated), you should use `read.delim()`. +When find yourself trying to load a tabular data file that consists of plain text, but has some idiosyncratic difference from a csv (e.g., it is tab-separated instead of comma-separated), you may want to use `read.delim()`. -How will you know what to use? Get to know your data first! Seriously, try opening it the file (or at least opening up part of it) using a text editor and/or spreadsheet software. Looking at the "raw" plain text can help you figure out what arguments you need to use to make the data load up exactly the way you want it. +Note that the Tidyverse has some related functions, namely `read_csv()` that can also import csv's very efficiently and with helpful defaults to try and guess what kinds of variables you're working with. The guesses are usually pretty good! Here's an example using `read_csv()` + +```{r} +library(tidyverse) + +yet.more.cars <- read_csv(url("https://communitydata.cc/~ads/teaching/2019/stats/data/week_03/mtcars.csv")) + +yet.more.cars + +table(yet.more.cars == my.mtcars) +``` + +How will you know what to use? The best practice is always to get to know your data first! Seriously, try opening it the file (or at least opening up part of it) using a text editor and/or spreadsheet software. Looking at the "raw" plain text can help you figure out what arguments you need to use to make the data load up exactly the way you want it. For example, you might notice that my import of the mtcars.csv file introduces an important difference from the original `mtcars` dataset. In the original `mtcars`, the car model names are `row.names` attributes of the dataframe instead of a variable. @@ -65,8 +77,7 @@ head(mtcars) row.names(mtcars) ``` - -Since it's the first column of the raw data, you can fix this with an additional argument to `read.csv`: +Since it's the first column of the raw data, you can fix this with an additional argument to `read.csv` (I'm sure there's also a way to do this with `read_csv()` but I didn't look it up in time to include in this tutorial): ```{r} @@ -226,9 +237,9 @@ Again, you could run `row.names(cyl.conditional.means)` <- NULL` to reset the ro By now you might have noticed that a some of my code in this section replicates parts of the output that the Tidyverse example above created by default. As I said earlier, the Tidyverse really shines when it comes to tidying data (shocking, I know). There are also functions ("verbs") in the Tidyverse libraries to perform all of the additional steps (such as sorting data by the values of a column). If you are interested in doing so, check out more of the [Tidyverse documentation](https://tidyverse.org/) and the [Wickham and Grolemund *R for Data Science* book](https://r4ds.had.co.nz/) listed among the course resources. -## Working with dates +## Working with dates (in Base R) -Date and time objects are another more advanced data class in R. Managing date and time data can be a headache. This is because dates and times tend to be formatted inconsistently and are usually given to you as character variables, so you will need to transform them into a format that R can "understand" as dates. There are many packages for working with dates and times, but for now I'll introduce you to the Base R way of doing so. This uses a data type formally called "POSIX" (no need to worry about why it's called that). +Date and time objects are another more advanced data class in R. Managing date and time data can be a headache. This is because dates and times tend to be formatted inconsistently and are usually given to you as character variables, so you should know how to transform them into a format that R can "understand" as dates. There are many packages for working with dates and times, but for now I'll introduce you to the Base R way of doing so. This uses a data type formally called "POSIX" (no need to worry about why it's called that). To build up an example, I'll create some date-time values, add a little noise, and convert them into a character vector: @@ -249,10 +260,10 @@ as.POSIXct(some.hours) If things aren't formatted in quite the way R expects, you can also tell it how to parse a character string as a POSIXct object: ```{r} -m <- "2019-02-21 04:35:00" -class(m) +sometime <- "2019-02-21 04:35:00" +class(sometime) -a.good.time <- as.POSIXct(m, format="%Y-%m-%d %H:%M:%S", tz="CDT") +a.good.time <- as.POSIXct(sometime, format="%Y-%m-%d %H:%M:%S", tz="CDT") class(a.good.time) ``` Once you have a time object, you can even do date arithmetic with `difftime()` (but watch out as this can get complicated): @@ -262,6 +273,22 @@ difftime(Sys.time(), a.good.time, units="weeks") ``` This calculated the number of weeks elapsed between the current time and an example date/time I created above. +### `as.Date` vs. `as.POSIX()` + +The `as.Date()` function provides an alternative to `as.POSIX()` that is far more memorable and readable, but far less precise. Note that it truncates the time of day and the timezone from the ouput. I'll use the same + +```{r} +a.good.time <- as.Date(sometime, format="%Y-%m-%d %H:%M:%S", tz="CDT") +class(a.good.time) +a.good.time +``` + +### An easier way (most of the time) in the Tidyverse + +The Tidyverse (via the `lubridate` package) usually handles dates and times quite well. When you need to import and work with date and time objects, you may find it best to try Tidyverse data import tools (e.g., `read_csv()`) as a starting point for this reason. + +I *highly recommend* reading [this chapter](https://r4ds.had.co.nz/dates-and-times.html) of Wickham and Grolemund on dates and times as they introduce a number of challenges and nuances that can befuddle even the most brilliant statistical programmers. + # Creating (repeated) sequences, distributions, and samples The *OpenIntro* reading this week introduces some core concepts and tools for probability. R has a number of functions that you can use to illustrate these concepts, replicate examples, and generate simulations of your own. This section of the tutorial introduces a few useful starting points. You may also find these snippets and functions valuable for managing real data.