0% found this document useful (0 votes)
12 views

R Tutorial

1. The document provides an overview of basic R concepts including variables, data types, operations, functions, vectors, lists, and data frames. 2. Examples are given for creating variables and assigning values, performing mathematical operations, using logical operators, defining functions, making vectors and lists, and working with data frames. 3. Key aspects of R covered include naming conventions for variables, data types and their possible values, function signatures, indexing and slicing vectors, retrieving elements from lists, and printing specific parts of data frames.

Uploaded by

lola
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

R Tutorial

1. The document provides an overview of basic R concepts including variables, data types, operations, functions, vectors, lists, and data frames. 2. Examples are given for creating variables and assigning values, performing mathematical operations, using logical operators, defining functions, making vectors and lists, and working with data frames. 3. Key aspects of R covered include naming conventions for variables, data types and their possible values, function signatures, indexing and slicing vectors, retrieving elements from lists, and printing specific parts of data frames.

Uploaded by

lola
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 1 Basic information

The left pannel is the console where I can type R codes and run it by pressing enter.

On the top right there is environment which shows all the variables that are being used. This is
where you can check if I preformed the code correctly.

Bottom right are the tabs on the laptop where I can upload files from in R. Clicking on plot will show
the last plot I made.

When opening a file click on the green + on the top left corner and choose R script; if you click on the
green + with R it will open a new project.
Chapter 2 reporting in Rmarkdown
Exercise 1 =

# = main title

## = second title

**bold** = to type in bold

**italics** = to type in italics

Markdown sheet = https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

My creations = https://dillinger.io/

1. Using codes

‘some code’ = is a code

in a code block “‘ = 3 x ‘ is a block code

running the code =“‘{r} print(4+4) “‘


Exercise 2 =

First to create a markdown you click on the green + and choose markdown. A code always starts
with

“ ‘{r}

The code

“‘

1. eval = showing the code. eval = FALSE


2. Include = the code and output to be shown. Include = FALSE
3. Echo = include the output but not the code. Echo = FALSE
4. Message and warning = Hide the warning. Message = FALSE or Warning = FALSE
5. Fig.cap and tab.cap = fig.cap = "A figure"

Example 1 =

Importing data = file -> import data -> copy code in R markdown file
Chapter 3 Basic R: Operations, functions and variables
Variables = name, type and value

1. Name = it cannot start with a number and not all characters are allowed. No abbreviations
unless these are incredibly common abbreviations.
- Dot separation: miles.per.gallon
- Camel case: milesPerGallon
- Snake case: miles_per_gallon

So a variable like 2pairs is not possible since it starts with a number. However, two.pairs is possible.
Two pairs is not possible cause spaces should be filled with _.

2. Type = Data types restrict the amount of information a variable can contain. A logical
variable (also called boolean) has two options: TRUE and FALSE. Every value type has
restrictions on the kind of contents it allows for. You will encounter the following, from strict
to lenient.
- Logical, TRUE or FALSE (equivalent of 0 and 1)
- Integer, any whole number (no decimals allowed)
- Numeric, any number (decimals allowed)
- Character, any character allowed.
3. Value = The value has to fit within the possible values that the data type can take

Exercise 1 creating a variable.

R=

Word (variable) <- word (value)

- So, I typed miles_per_gallon <- 6.4. And if I type miles_per_gallon


it will automatically give the value 6.4
- If I would overwrite the value 6.4 into 10.5 and then enter miles
per gallon it will autatically update the value 6.4 into 10.5

Overwriting a value = so if I first did animal <- cat and then do animal <- dog
the first value is overwritten

Rstudio =

I can also do this in Rstudio, when typing in the script miles_per_gallon <- 6.4 and then select the
line and enter run it will show this;
Exercise 2 operations

Mathematical operators

+ addition

- subtraction

* multiplication

/ division

ˆ or ** exponentiation

%% modulo

%/% floor division (division result without decimals, always rounded down)

Example 1 =

First number = 47

Second number = 3.14

Entered in R; first_number <- 47 second_number <- 3.14

Print(first_number + second_number)

- R directly knows the anwers

Logical operators

== for equality

!= for difference

> and >= Greater than and greater equal

< and <= Smaller than and smaller equal

Example 2 =

How can you check is something is true or false?

- Enter the variable with its value


- e.g first_number > second_number

In the next example I asked R if the first number equals


to the second number and it said false. Then I did a
multiple code if the first number is equal to the third
number or if the first number is greater than the third
number. R said true but this is based on only one of the two codes.
How can you check is a number is odd or even?

1. x <- 1:5 (run)


2. x_logical <- x %% 2 == 0 (run)
- when doing this R shows what numbers give a false and what numbers give a true result
from the code
3. when entering x_even <- x[x_logical] it gives the
numbers 2 and 4.
4. When entering x_odd <- x[!x_logical] it gives the
numbers 1,3 and 5

Example 3

Combining logicals like && | and

Did some combinations with && and | which is


shown in the figure on the right.

When I did amount_of_jeans && (!


amount_of_shoes) > 30 it said false. The ! illustrates that R should not take into account that
category.

Exercise 3 functions

print() is a function, as is class().

Function signature =

- Function name. Same naming conventions apply as for variables.


- Function arguments. The input a function takes. These are named and names should be
informative.
- Return type. The thing a function outputs (returns). In R, this is not strictly part of the
signature, but is always in the documentation.

Structure function =

function_name <- function(arguments) {

# code

return(variable) }

Example 1 =

square_root <- function(number) {

# Takes the square root of a function

return(number ** .5) }

Then when I enter square_root and fill in as a number 5 it will give the square root of 5
Exercise 4 R internal help system

?function_name

??search_term

This opens the help file with possible solutions. Other possibilities are the internet.
Chapter 4 Vectors, list, and data frames
Vectors
Making a vector = c(…)
- Heights <- c(1,92, 1,87, 1,63, 1,84)
- Names(height) <-c(“Max, “Bob”, “Rose”,
“Emily”)

When clicking on height data in the environment it shows


this;

Index
When coding print(heights[3]) R automatically gives the third value 1.63
Slice = [first_index:last_indext]
Changing values = number[2] <- 9

Example 1 =

Created a vector Got_women and then printed the


numbers 3:5 and R directly gave the names.

Created a new vector with additional names 7 and TRUE = this vector contains a number instead of a
word.

List
There are three ways to retrieve elements from a list. Depending on (a) the type of list and (b) the
type of output you expect, you can choose from the following.

How to list = variable <- list (value=value, value2=value2, value3=value3)

Example 1 =

A list of name, type and value


awesome_women$name = it gives Arya, Xena and Kathryn Janeway

Dataframe

Print x = gives all the words

Name data 1 = gives the first word

Print names data = all the changed names

Adding columns

# Extract vector to print print(data$mpg)

# Add extra column: data$hp_per_1000lbs <- data$hp / data$wt

# Replace single value data$mpg[1] <- 22

Index [1:3,] = only the rows 1 till 3

Index [,”mpg”] = gives all the rows

Example 1 =

1. Loaded the awesome women code


2. Added a new variable ethnicity <- c(Turkish, Chinese, Canadian)
3. Changing the ages I did = age[1:3]<-
c(18,38,40)
4. When opening the mpg code in R and run it, it
resulted in this. In the data.frame Camaro Z28
drives 13.8 milles per gallon
Advanced indexing

numbers <- c(1, 2, 4, 8, 16, 24, 48)

print(numbers > 5) = this only says true or false if a


number is larger than 5.

print(numbers[numbers > 5]) = this shows what the numbers are.

Example 1 =

How to find out if numbers are odd or even. First add the variable and its values. Then say numbers
logical when the number %%2 = 0. Then say numbers even and numbers odd! It should give the
numbers that are odd and even.

numbers_odd[1:3]<-c(4,6,8) = this helps to add 1 to all the odd numbers but it isn’t done with the if
else function…

Extra functions
%in% can show whether an something is part of something else, which looks for where elements
with certain characteristics are.

print(5 %in% c(1, 3, 5, 7, 9, 11)) # TRUE -> 5 is part of the range

print(8 %in% c(1, 3, 5, 7, 9, 11)) # FALSE -> 8 is not part of the range

Example 1=

which(fruit_basket == "4") = tried this to find out what on position 4 is but didn’t work out

You might also like