RMarkdown

Getting Familiar with RStudio

1. Numbers and Operators

Arithmetic

Arithmetic Numbers and basic arithmetic operators (+,-, *,/) behave as expected. Example: when you type “2+5” in your console, you will get “7”.

#Arithmetic numbers examples are here!
2+3
[1] 5

Logic

R can also perform logical operations. Example: when you type 5>3, you will get “True”.

5>4
[1] TRUE

Assignment

my_hobby <- “cooking”

my_hobby

my_hobby <- "acting"

my_hobby
[1] "acting"

The Dollar Sign

Used to select variables within the datasets

Example: trees$Height

trees$Height
 [1] 70 65 63 72 81 83 66 75 80 75 79 76 76 69 75 74 85 86 71 64 78 80 74 72 77
[26] 81 82 80 80 80 87

Now, you try: Select Grith and assign to variable tree_grith and print that in the console.

trees$Girth
 [1]  8.3  8.6  8.8 10.5 10.7 10.8 11.0 11.0 11.1 11.2 11.3 11.4 11.4 11.7 12.0
[16] 12.9 12.9 13.3 13.7 13.8 14.0 14.2 14.5 16.0 16.3 17.3 17.5 17.9 18.0 18.0
[31] 20.6

Question Mark

Used to get help

?trees

?trees

2. Object & Data

Object: Variables in our dataset, everything in R environment is an object.

  • A vector, a data frame, a list, and a matrix can all be defined as an object.

  • Create objects with assign sign “<-” R is case-sensitive

  • Object names cannot start with a number, should be short and explicit.

  • Characteristics of an object, class Your object can include different types of data including numeric, character, etc.

Example: my_grade<-90+90

class(my_grade)

my_grade<-90+90

Data Types

Vector: a collection of ordered homogeneous elements

my_vector<–c(1,2,3,4)

my_vector

my_vector<--c(1,2,3,4)

List: a vector with possible heterogeneous elements

my_list<- c(1, “coffee”, 4)

my_list

my_list<- c(1, "coffee", 4)

Matrix: a vector with two-dimensional shape information

my_matrix <- matrix(1:9, nrow=3, ncol=3)

my_matrix

my_matrix <- matrix(1:9, nrow=3, ncol=3)

Data frames: a list with possible heterogeneous vector elements of the same length. The elements of a data frame can be numeric vectors, factor vectors, and logical vectors.

my_df <- trees

My_df <- iris

my_df <- trees

Notes:

Aside from being a certain class, objects can have other attributes, such column names and dimensions:

colnames(my_df)

dim(my_df)

colnames(my_df)
[1] "Girth"  "Height" "Volume"

And can even be used with operators!

Try this: my_grade*my_matrix

3. Functions- Built in

Simple examples of built-in functions are seq(), mean(), max(), sum(), and paste()….

Create a sequence of number seq(32,65)

seq(32, 65)
 [1] 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
[26] 57 58 59 60 61 62 63 64 65

Find mean of numbers from 25 to 82 mean(25:65)

(25:65)
 [1] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
[26] 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

Sum of numbers from 25 to 82 sum(25:82)

(25:82)
 [1] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
[26] 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
[51] 75 76 77 78 79 80 81 82

4. Functions – user defined

Functions: a set of statements organized together to perform a specific task.

function_ name<-function(arg1, arg2,….)

{ Function body }

Example:

add_number<-function(number1, number2)

{ number1+number2 }

add_number (1,2)

add_number <- function(number1, number2) {
  number1 + number2
}

5. Packages

Collection of R code that contain functions, data, and/or documentation.

Install packages

Install. packages(“tidytext”)

Click and install the package from R studio environment Loading a package

Library(package_name)

library(tidytext)

library(tidytext)