Documente Academic
Documente Profesional
Documente Cultură
Dec-18
Introduction
Official Site:
– https://www.r-project.org/
– Download from
http://cran.r-project.org/mirrors.html
– Base Distribution
https://cran.r-project.org/bin/windows/base/
– Current version: 3.5.1
– 32-bit Vs. 64-bit: as per individual choice
• Corporate environment: As per production environment
RStudio Desktop (V 1.1.463): popular IDE to run R programs
– Interface to the R console
– Has a free license
– Also has a non-install version
– https://www.rstudio.com/products/rstudio/download/
3
Confidential |
Usage
5
Confidential |
Getting Started
Incomplete command
– Prompt changes to "+": continues to take input
– Until the command is syntactically complete
Execute R commands stored in an external file:
– Escape "\" if full path is provided
source("example.R")
6
Confidential |
Getting Started
help() function
> help.start() # general help
> help(Syntax)
> ?Syntax
> help.search("histograms")
> ??"histograms"
7
Confidential |
Reserved Words
9
Confidential |
> typeof(5)
Constants > typeof(5L)
> typeof(5i)
> 0xff
> 0XF + 1
Entities whose value cannot be altered
> 'example'
– Basic types: numeric, character > typeof("5")
Numeric Constants: All numbers
– Can be of type integer, double or complex
– Can be checked with typeof()
– Numeric constants followed by L are regarded as integers
• Those followed by i are regarded as complex
• Those preceded by 0x or 0X are interpreted as hexadecimal numbers
Character Constants
– Can be represented using either single (') or double quotes (") as
delimiters
10
Confidential |
Built-in Constants
> LETTERS
> letters
> pi
> month.name
> month.abb
> pi <- 56
> pi
11
Confidential |
Operators
12
Confidential |
Logical Operators
13
Confidential |
Data Types
15
Confidential |
Operations on Vectors
16
Confidential |
Vectors
17
Confidential |
Vectors
Modify Vectors
> x<-c(-3, -2, -1, 0, 1, 2); x
> x[2] <- 0; x
> x[x<0] <- 5; x
> x <- x[1:4]; x
> x<-c(-3, -2, NA, 0, 1, 2); x
> x[is.na(x)] <- 5; x
18
Confidential |
Data Structures
21
Confidential |
Matrix
22
Confidential |
Modify Matrix
Transpose: t(x)
Add row or column using rbind() and cbind()
– Remove through reassignment
x<-cbind(x, c(1, 2, 3)); x
x<-rbind(x, c(1, 2, 3)); x
x <- x[1:2,]; x
Modify dimension
x<-1:6; dim(x)<-c(2,3); x
dim(x) <- c(3,2); x
dim(x) <- c(1,6); x
23
Confidential |
Data Structures
Data Frame: special type of list - every list element has same length
– Usually created by read.csv() and read.table(): data import
– Can be converted to a matrix: data.matrix()
• If all columns are of the same type
> dat <- data.frame(id = letters[1:10], x = 1:10, y = 11:20); dat
> class(dat); length(dat); str(dat)
> dat[1, 3]; dat[["y"]]; dat$y
– Number of rows and columns: nrow(dat); ncol(dat)
– Other useful functions:
> head(); tail(); dim(); nrow(); ncol(); str(); names()
> sapply(dataframe,class)
– data.frame() function converts character vector into factor
• Suppress: stringsAsFactors=FALSE
x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John", "Dora"),
stringsAsFactors = FALSE)
26
Confidential |
Data Frame
– Adding Components
• Rows: rbind() x<-rbind(x,list(1,16,"Paul"));x
• Columns: cbind() x<-cbind(x,State=c("NY","FL"));x
x$State <- c("FL","OH","NY","WA"); x
• Like a list:
– Deleting Components
• Columns x$State <- NULL; x
• Rows x <- x[-1,];x
27
Confidential |
Factors
28
Confidential |
Factors
library(foreign)
mydata <- read.dta("c:/mydata.dta")
mydata <- read.systat("c:/mydata.dta")
library(Hmisc)
mydata <- spss.get("c:/mydata.por", use.value.labels=TRUE)
mydata <- sasxport.get("c:/mydata.xpt")
library(xlsx)
mydata <- read.xlsx("c:/myexcel.xlsx", sheetName = "mysheet")
30
Confidential |
Getting Data
31
Confidential |
Writing Data
carSpeeds$Speed[3] <- NA
32
Confidential |
Descriptive Statistics
33
Confidential |
Thank you