X-Git-Url: https://code.communitydata.science/stats_class_2020.git/blobdiff_plain/5adecb998e3ae7930f1c09da7490bf28d2742177..031b7bd587ecb01c5628cc1a282ca121b06c381c:/r_tutorials/w03a-R_tutorial.html?ds=sidebyside diff --git a/r_tutorials/w03a-R_tutorial.html b/r_tutorials/w03a-R_tutorial.html index 1608264..9ee7dd2 100644 --- a/r_tutorials/w03a-R_tutorial.html +++ b/r_tutorials/w03a-R_tutorial.html @@ -1621,6 +1621,18 @@ my.mean <- function(z) { return(out.value) } +
+

4 Specifying variable classes with data import

+

Aaron C. asked a question about whether/how you might specify variable classes when you’re importing data. Aaron S. punted at the time, so here’s a slightly more specific reply.

+

The short answer is, “yes, R can do this.” The details depend on exactly which function you use to import the data in question (and that depends partly on the file format…etc.).

+

The most helpful place to look for more information is the help documentation for whatever import function you might be working with. For example, the read.csv() function that gets introduced in the next R tutorial takes an optional argument for colClasses that allows you to specify a vector of classes (e.g., c("character", "factor", "integer", "character")) corresponding to the classes you want R to assume for each incoming column of the data.

+

Try reading help(read.csv) and look at the documentation for the colClasses argument to learn more.

+
+

4.1 R guesses the classes of variables when you import them

+

Aaron and Nick both made comments about R guessing the classes of variables when you import data. The nature and quality of these guesses can depend on the import function there too.

+

Most Base R import stuff makes guesses you might think of as somewhat brittle (assumptions (e.g., looking at just the first five values to inform the guess. In contrast, the Tidyverse data import commands usually use a larger and more random sample of values from each column to make guesses (which are therefore much better).

+
+