Editing Statistics and Statistical Programming (Winter 2017)/R lecture outline: Week 1
From CommunityData
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 23: | Line 23: | ||
** logical: TRUE or FALSE (all caps) | ** logical: TRUE or FALSE (all caps) | ||
* functions: contains parentheses right after the variable name | * functions: contains parentheses right after the variable name | ||
** functions take some input (called an argument) and provide some output (called the output or something the return value | ** functions take some input (called an argument) and provide some output (called the output or something the return value) | ||
** there are many built in functions including: | ** there are many built in functions including: | ||
*** sqrt() | *** sqrt() | ||
Line 34: | Line 31: | ||
*** ls() | *** ls() | ||
*** check your reference card for many, many more | *** check your reference card for many, many more | ||
* vectors: you can think of a vector as like a list of things that are all the same | * vectors: you can think of a vector as like a list of things that are all the same time (lists, which will come to letter, actually refer to lists of things that might be of different types!) | ||
** in R, all variables are vectors! although many have just one thing in them! that's why it prints out [1] next to every numbers | ** in R, all variables are vectors! although many have just one thing in them! that's why it prints out [1] next to every numbers | ||
** you can make vectors with a special function: c(), like ages <- c(36, 4, 35) | ** you can make vectors with a special function: c(), like ages <- c(36, 4, 35) | ||
Line 43: | Line 40: | ||
*** more complex: ages[1:2] | *** more complex: ages[1:2] | ||
*** assignment through indexing: ages[1] <- 20 | *** assignment through indexing: ages[1] <- 20 | ||
** vectors can names for elements! we can set those with names(): | ** vectors can names for elements! we can set those with names(): | ||
*** names(ages) | *** names(ages) | ||
Line 49: | Line 45: | ||
*** once we do that, we can index with names: ages["mako"] | *** once we do that, we can index with names: ages["mako"] | ||
** many functions are particularly useful on vectors with multiple elements: | ** many functions are particularly useful on vectors with multiple elements: | ||
*** | *** sum() | ||
*** | *** mean(); sd(); median(); var(); IQR() etc() | ||
*** | *** length() | ||
*** head() | |||
*** table() | |||
* more advanced variables types: | |||
** factors: for categorical data | |||
*** make with factor("mako", "mika", "mako") | |||
*** you can create factors from characters with as.factor() | |||
** also think about: dates with POSIXct(), ordered() — really just a type of factor for ordinal data | |||
* using logical vectors to index and recode data: | * using logical vectors to index and recode data: | ||
** comparison operators will return logical variables: rivers > 300; rivers < 300; rivers <= 320; rivers == 210; rivers != 210 | ** comparison operators will return logical variables: rivers > 300; rivers < 300; rivers <= 320; rivers == 210; rivers != 210 | ||
Line 59: | Line 62: | ||
** boxplot() — boxplots | ** boxplot() — boxplots | ||
** hist() — draw histograms | ** hist() — draw histograms | ||
* | ** density() — density plots | ||
* | * installing new pacakges and loading new datasets: | ||
* installing new | |||
** install.packages("UsingR") | ** install.packages("UsingR") | ||
** install.packages("openintro") | ** install.packages("openintro") | ||
*** library(UsingR) no quotes! | *** library(UsingR) no quotes! | ||