1 May, 2017
ConsoleFile > New File > R Script
loadingData.R.R:
R commands in this file
Consolebash)R commands, and comments
R script start with ## are ignored by the Consolepackages (e.g. base)
R always loads stats, graphics, grDevices, utils, datasets, methods and base at start-upPackages Tab to load packagesTools > Global Options > Packages > Uncheck Enable Packages Panedplyr package laterlibrary(dplyr)
NB: We have not yet sent this command to the Console
Ctrl + Enter or Mac Command + Enter
NB: If you don't have dplyr installed:Tools > Install Packages... > then type dplyr and hit Install
RStudio has a handy GUI for loading data into your R Environment
Environment TabImport Dataset (From CSV...Browse to data/toothData.csvStop there!!!
Code Preview Boxlibrary(dplyr)ImportThe code we copied has 3 lines:
library(readr)
read_csv()toothData <- read_csv("~/Documents/R-Training-20170501/data/toothData.csv")
R EnvironmenttoothData by using the file name.library(readr)
read_csv()toothData <- read_csv("~/Documents/R-Training-20170501/data/toothData.csv")
R EnvironmenttoothData by using the file name.View(toothData) Open a preview in a familiar Excel-like format
View(toothData)toothData
## # A tibble: 60 × 3 ## len supp dose ## <dbl> <chr> <chr> ## 1 4.2 VC Low ## 2 11.5 VC Low ## 3 7.3 VC Low ## 4 5.8 VC Low ## 5 6.4 VC Low ## 6 10.0 VC Low ## 7 11.2 VC Low ## 8 11.2 VC Low ## 9 5.2 VC Low ## 10 7.0 VC Low ## # ... with 50 more rows
tibble is a type of R object called a data.frame
tbl_df based on an SQL tabletibbledata.frame with a couple of nicer feature
data.frame dumps everythingdata.frame?vectordata.frame?vectorA : Now we have to care about what columns contain numbers, words etc.!
$ after the nametoothData$len toothData$supp toothData$dose
supp and dose represent the supplement method and dosage of vitamin C given to guinea pigs
len represents the length of their incisorssupp and dose be categorical variables?
Console# Convert the supplement method and dosage to factors toothData$supp <- as.factor(toothData$supp) toothData$dose <- as.factor(toothData$dose)
toothData
<fctr>read.csv()
characters to factorsA: Delete those lines from the script, and run it from the top again.
A data.frame is a special case of an R object called a list
data.frame, each column was a vector
matrix is like a 2D vector
matrix using the $ symboltoothData into a matrix?as.matrix(toothData)
toothData into a matrix?A: The numeric values would become character values
install.packages("packageName")