Appendix A — Rcode
Here is a list of the R code we use in this class.
A.1 R Code : Creating Variables in R
A.2 Numeric Variables in R
| Code | Description |
|---|---|
|
variable = an object that you will define in R <- = “assign”; tells R to save whatever comes on the right to whatever object is on the left. c = combine : tells R to combine whatever happens in the parentheses () = parentheses to group related terms # = what you store in the variable; each item should be separated by a comma and space. |
hist(dat$variable) |
For continuous variables : draws a histogram. |
A.3 String Variables
|
variable = an object that you will define in R <- = “assign”; tells R to save whatever comes on the right to whatever object is on the left. c = combine : tells R to combine whatever happens in the parentheses () = parentheses to group related terms # = what you store in the variable; each item should be separated by a comma and space. |
|
as.factor() # converts a string variable into a categorical factor |
variable <- as.factor(variable) |
# “saves” this conversion as the original variable |
plot(dat$variable) |
For categorical variables : draws a barplot. For continuous variables : illustrates values of the variable (y-axis) as a function of their index (x-axis). |
A.5 R Commands for Visualizing Data
| R Command | What it Does |
summary(dat) |
Reports descriptive statistics for all variables in the dataset. |
summary(dat$variable) |
Reports descriptive statistics for a categorical variable (frequency / number of individuals in each level) or continuous variable (mean, range, etc.) |
as.numeric(dat$variable) |
Makes the variable numeric (for continuous graphs) |
as.factor(dat$variable) |
Makes the variable a categorical factor (for categorical graphs) |
dat$variable <- as.factor(dat$variable) |
Assigns the as.factor output to the original variable. (In other words, this saves your new categorical factor variable by overwriting the old one.) |
plot(dat$variable) |
For categorical variables : draws a barplot. For continuous variables : illustrates values of the variable (y-axis) as a function of their index (x-axis). |
hist(dat$variable) |
For continuous variables : draws a histogram. |
par(mfrow = c(i, j)) |
Splits your graphics window into i rows and j columns. |
A.6 R Code : Descriptive Statistics
Below is a list of code we’ll use to calculate descriptive statistics in R.
| R Command | What It Does |
summary(dat) |
Reports descriptive statistics for all variables in the dataset. |
summary(dat$variable) |
Reports descriptive statistics for a continuous variable. Reports frequency for a categorical variable. |
mean(dat$variable, na.rm = T) |
Reports the mean (average) of a variable; you must include the na.rm = T argument if there is missing data (otherwise R will return NA as the result). |
median(dat$variable, na.rm = T) |
Reports the median (middle point) of a variable. |
range(dat$variable, na.rm = T) |
Reports the lower limit and upper limit of the variable. |
sd(dat$variable, na.rm = T) |
Reports the standard deviation of the variable. |
|
Draws a line on a plot or histogram at specified values (e.g., this draws a vertical line at the mean of dat$variable. You can replace v with h to draw a horizontal line. We will use abline() later in the semester in a different way. |
par(mfrow = c(i, j)) |
Splits your graphics window into i rows and j columns (replace i and j with numbers) |