0% found this document useful (0 votes)
4 views24 pages

Quick Start Guide to R and R Studio

Machine learning notes

Uploaded by

Melody Whelan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Quick Start Guide to R and R Studio

Machine learning notes

Uploaded by

Melody Whelan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R and R studio

Oliver Hinder

12/23/2020
R and R studio

I R is a data analysis environment


I The programming language
I The system of libraries and books around it.
I R Studio is an Integrated Development environment.
I If you are already a computer programmer, you can use your editing
tools to work in R.
I R Studio includes an editor, console, environment viewer, history
I Access to help files
I Goal of this lecture: a quick start guide to R if either
I you have programmed before but not in R
I you are a little rusty in R
R Markdown

I R Markdown is a format for writing documents and presentations.


I This is an R Markdown presentation.
I For more details see [Link]
I When you click the Knit a document will be generated that includes
both content as well as the output of any embedded R code.
I HWs and projects should be done in Rmarkdown
Action item: Open this presentation
(IntroductionToSoftwareTools/[Link]) and try knitting it.
Cheatsheets

I Click Help in the menu bar then Cheetsheets then click the
cheatsheet you want to look at.
Action item: Explore R Markdown Cheat Sheet, RStudio IDE Cheat
Sheet and R Markdown Cheat Sheet
Interactive R

R can be used as a calculator


3+5

## [1] 8
Run a function
x = rnorm(20)
y = rexp(20)
plot(x,y)
2.0
1.5
y

1.0
0.5
0.0

−2 −1 0 1

x
What does a function do?

Type ? in front of a function in the console to find out what a function


does
?rnorm
?rexp
Creating your own function

add_two <- function(input_number) {


output_number <- input_number + 2
return(output_number)
}
add_two(3)

## [1] 5
Adding comments

# This is a comment in R.
# Remember use comments to document your code.
Some data types

Some standard data types


I Logical
TRUE == FALSE # True equals false?

## [1] FALSE
!FALSE # Not false?

## [1] TRUE
FALSE | TRUE # Either true or false?

## [1] TRUE
More data types
I Floats
x <- 1.0
# Double means a floating point number stored with 64 bits of pr
# [Link]
typeof(x)

## [1] "double"
class(x)

## [1] "numeric"
y <- 1
class(y) # This is still a floating point number

## [1] "numeric"
I Integers
z <- 1L # Add L to specify that is an integer
class(z)

## [1] "integer"
More data types

I Characters
my_string <- "hello"
class(my_string)

## [1] "character"
print(my_string)

## [1] "hello"
I Dates
course_start <- [Link]("2020-01-18")
course_end <- [Link]("2020-05-01")
course_length <- course_end - course_start
print(course_length)

## Time difference of 104 days


Some data structures

I Vectors (numbers)
I You can apply functions over entire vectors (vectorization)
a <- c(3, 5, 3, 7, 10)
b = c(1, 2, 3, 4, 5)
1/b

## [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000


sum((a-mean(a))^2)/(length(a)-1)

## [1] 8.8
var(a)

## [1] 8.8
More data structures

I Factors are more efficient ways of storing ordered categorical data


I Each unique categorical value is replaced by an integer
I Less memory that storing each string
factored_data <- factor(c("rain", "sun",
"rain", "rain",
"sun"))
factored_data

## [1] rain sun rain rain sun


## Levels: rain sun
Sequences

I You can construct a sequence like a for loop


seq(from=-5, to=5, by=2)

## [1] -5 -3 -1 1 3 5
seq(-5, 5, 2)

## [1] -5 -3 -1 1 3 5
seq(-5, 5, by=2)

## [1] -5 -3 -1 1 3 5
?seq

I You can construct a sequence using :


-2:4

## [1] -2 -1 0 1 2 3 4
Matrices

I Create a matrix from a single vector


mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
mat

## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
More matrices

I Create matrix by binding columns together


mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2

## [,1] [,2]
## [1,] "1" "dog"
## [2,] "2" "cat"
## [3,] "3" "bird"
## [4,] "4" "dog"
I Create matrix by binding rows together
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3

## [,1] [,2] [,3] [,4]


## [1,] 1 2 4 5
## [2,] 6 7 0 4
Data frames

I Like a matrix, but you can have different data types.


I Columns can be named
I Data frames are the primary data structure you will work with.
I The better R packages all work on data frames.
I ggplot2, tidyr, reshape, dplyr
students <- [Link](c("Cedric","Fred","George","Cho",
"Draco","Ginny"),
c(3,2,2,1,0,-1),
c("H", "G", "G", "R", "S", "G"))
Data frames
I naming the columns:
names(students) <- c("name", "year", "house")
students

## name year house


## 1 Cedric 3 H
## 2 Fred 2 G
## 3 George 2 G
## 4 Cho 1 R
## 5 Draco 0 S
## 6 Ginny -1 G
I Access each column by its name
students$name

## [1] "Cedric" "Fred" "George" "Cho" "Draco" "Ginny"


students$year

## [1] 3 2 2 1 0 -1
students$house
Dataframes from vectors

I You can also create a dataframe from a set of vectors


hogwart_students <- [Link](name = students$name,
year = students$year,
house = students$house)
hogwart_students

## name year house


## 1 Cedric 3 H
## 2 Fred 2 G
## 3 George 2 G
## 4 Cho 1 R
## 5 Draco 0 S
## 6 Ginny -1 G
Tips for coding

I use descriptive variable names, e.g., hogwarts_students rather than


x
I don’t be afraid to use Google/stack overflow to figure out issues
I play around in the console to understand how things work
I come to office hours!
Installing packages

What makes R powerful is the available packages.


[Link]("name_of_package_goes_here")

For this course:


# paste this into command line
[Link](c("DMwR", "rattle"),
dependencies = c("Depends", "Suggests"))

will install most of the packages we need.


R studio will often detect when a package is not installed and suggest you
install it.
Modern dive
As an introduction to R packages we will find useful in this course we
cover Chapter 1-4 of modern dive before switching to the applied
predictive modeling textbook for the core course content.
Learning checkpoint

I Read Chapter 1 of Modern Dive


I Create a Rmarkdown document containing the answers to the
learning check points in Chapter 1 of ModernDive
I Submit the Rmd file with your answers via canvas.

Common questions

Powered by AI

Factors in R optimize data handling by storing categorical data as integers rather than strings, significantly reducing memory usage . Each unique categorical value is replaced by an integer, making operations like sorting and comparison faster than with character vectors . This efficiency makes factors particularly useful in large datasets where memory and computation efficiency are crucial .

Vectors in R are a basic data structure that store elements of the same type and allow for vectorization where functions operate over entire vectors simultaneously . Data frames, on the other hand, can hold different data types in different columns, making them more versatile for real-world data analysis . Data frames are the primary data structure used for data manipulation and can be directly integrated with powerful R packages like ggplot2 and dplyr, enhancing data management capabilities .

Matrix construction in R using cbind and rbind functions allows for efficient organization of data by concatenating columns or rows, respectively, into matrices . This method facilitates structured data storage suitable for mathematical operations and analyses, as matrices allow for easy manipulation of data across rows and columns, providing a robust framework for dataset transformations and computational tasks .

R uses comments to provide explanations and context within the code, which aids in documentation and enhances collaboration by making the code more understandable to others . Through consistent commenting, team members can quickly comprehend the purpose and procedures of different code segments, facilitating easier collaboration and project handovers .

Creating functions in R involves defining a function using the function() keyword and capturing input parameters to perform customized operations, returning specific outputs . This process enhances coding efficiency by promoting code reuse, reducing redundancy, and allowing for modular code design where independent pieces of functionality can easily be used across different projects or analysis tasks .

R handles sequence generation using the seq() function or the ':' operator to create sequences with specific intervals . This capability is particularly useful when creating index sequences for iterative processes, simulating time series data, or generating predictors for model training in machine learning tasks, as it provides flexibility to generate sequences with customized start, end, and step parameters .

R Studio enhances the programming experience by providing an integrated development environment that includes features such as an editor, console, environment viewer, and history tracking, which are not available in basic text editors . This integrated setup allows for seamless workflow and easy access to help files and important tools, improving efficiency and effectiveness in coding tasks in R .

R Markdown facilitates reproducible research by enabling users to write documents and presentations that include both content and the output of embedded R code, ensuring that data analyses are documented alongside their corresponding explanations and results . The 'knit' function generates a comprehensive document, improving transparency and reproducibility in data analysis workflows .

Using descriptive variable names in R coding is considered best practice because it improves the readability and understandability of the code, making it easier for others (and oneself) to follow the logic and purpose of various segments over time . It also aids in code maintenance and debugging, as descriptive names provide context without needing extensive comments or documentation, facilitating collaboration and future edits .

Installing necessary packages in R is significant because it extends the base functionality of R, allowing users to apply a wider array of methods and tools tailored to specific analysis needs . Packages often encapsulate sophisticated functionalities, from data manipulation to modeling, enabling users to efficiently address complex data analysis tasks and improve productivity by leveraging community-contributed solutions .

You might also like