This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
##GIRE
a <- 10
b <- 2
print(a)
## [1] 10
print(b)
## [1] 2
print(c(a, b))
## [1] 10 2
# Operations
a + 2
## [1] 12
b * 2
## [1] 4
a / b
## [1] 5
a %/% 3 # Quotient
## [1] 3
a %% 3 # Remainder
## [1] 1
b ^ 3
## [1] 8
log(a) # Natural Log
## [1] 2.302585
log10(a) # Common Log
## [1] 1
exp(log(a))
## [1] 10
#functions
# R: THE COOLEST CALCULATOR
## Basics: Arithmetic and interacting with the interpreter
2 + 3 # addition
## [1] 5
2 - 3 # subtraction
## [1] -1
2*3 # multiplication
## [1] 6
2/3 # division
## [1] 0.6666667
2^3 # exponentiation
## [1] 8
#3 + 5*(3-) # user error
## Order of Operations: sounds like high school algebra
4^2 - 3*2
## [1] 10
1 - 6 + 4
## [1] -1
2^-3
## [1] 0.125
(4^2) - (3*2) # use parentheses to group, clarify
## [1] 10
4 + 3^2
## [1] 13
(4 + 3)^2
## [1] 49
#####vector################## ############################ ## vector (basic data structure that contains elements of the same data type arranged in a sequence) #Vectors #A vector is a basic data structure in R that holds elements of the same data type. You can create a vector using the c() function. Once a vector is defined, you can perform operations on it. For example,
a <- c(1, 2, 3)
print(a)
## [1] 1 2 3
a * 2
## [1] 2 4 6
b <- c(4, 5, 6)
## Operations on vectors
a + b
## [1] 5 7 9
c <- c(7, 8)
a + c # Error when lengths don't match
## [1] 8 10 10
d <-c(9, 10, 11, 12)
c + d
## [1] 16 18 18 20
## vector indexing
d[1]
## [1] 9
d[4]
## [1] 12
#####Application #######
## Creating a numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)
# Creating a character vector
character_vector <- c("apple", "banana", "cherry")
# Creating a logical vector
logical_vector <- c(TRUE, FALSE, TRUE)
#######LIST########### ##################### # List (more flexible data structure compared to a vector because it can store elements of different types)
a <- c(“A”, 1, TRUE) # Vector print(a)
str(a) # Check data structure b <- list(“A”, 1, TRUE) # List print(b) str(b)
b[1] b[1][[1]] b[2][[1]] b[3][[1]]
####################
######Dataframe######
####################
# Dataframe (table-like data structure that is used to store data in rows and columns)
``` r
df <- data.frame(group = c('A', 'A', 'A', 'B', 'B', 'B'),
value = seq(1, 6, 1))
df
## group value
## 1 A 1
## 2 A 2
## 3 A 3
## 4 B 4
## 5 B 5
## 6 B 6
#seq
?seq
x2 = seq(0,10,2)
x2
## [1] 0 2 4 6 8 10
x1 = seq(0,10,1)
x1
## [1] 0 1 2 3 4 5 6 7 8 9 10
####seeing the df############
str(df) # Check the structure of a dataframe
## 'data.frame': 6 obs. of 2 variables:
## $ group: chr "A" "A" "A" "B" ...
## $ value: num 1 2 3 4 5 6
dim(df) # Size of a data frame (the number of rows and columns)
## [1] 6 2
#to see what is row and column
matrix_data <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
df <- data.frame(group = c('A', 'A', 'A', 'B', 'B', 'B'),
value = seq(1, 6, 1))
df
## group value
## 1 A 1
## 2 A 2
## 3 A 3
## 4 B 4
## 5 B 5
## 6 B 6
str(df) # Check the structure of a dataframe
## 'data.frame': 6 obs. of 2 variables:
## $ group: chr "A" "A" "A" "B" ...
## $ value: num 1 2 3 4 5 6
dim(df) # Size of a data frame (the number of rows and columns)
## [1] 6 2
df[1, 1]
## [1] "A"
df[1, c(1, 2)]
## group value
## 1 A 1
df[1:3, ]
## group value
## 1 A 1
## 2 A 2
## 3 A 3
df[df$group == "A", ] -> a
a
## group value
## 1 A 1
## 2 A 2
## 3 A 3
df[df$group == "A", 'value'] -> b
b
## [1] 1 2 3
str(b)
## num [1:3] 1 2 3
#Lecture 06.21
#load the data
dat <-mtcars
#read the data
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
tail(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
View(mtcars)
names(mtcars)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
colnames(dat)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
str(dat)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
#find the unqiue values in the cyl column using unique
?unique
unique(mtcars$cyl)
## [1] 6 4 8
unique(mtcars$cyl)
## [1] 6 4 8
str(mtcars$cyl)
## num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
dat %>%
group_by(cyl) %>%
summarise(mean = mean(mpg))
## # A tibble: 3 × 2
## cyl mean
## <dbl> <dbl>
## 1 4 26.7
## 2 6 19.7
## 3 8 15.1
# install.packages("tidyverse") # Run this manually if needed
library(tidyverse)
dat <- mtcars
dat %>%
group_by(cyl) %>%
summarise(mean = mean(mpg))
## # A tibble: 3 × 2
## cyl mean
## <dbl> <dbl>
## 1 4 26.7
## 2 6 19.7
## 3 8 15.1
#Vector
animal_a <- c("pig", "cow", "horse", "rabbits")
print(animal_a)
## [1] "pig" "cow" "horse" "rabbits"
str(animal_a)
## chr [1:4] "pig" "cow" "horse" "rabbits"
class(animal_a)
## [1] "character"
#List
animal_b <- list("pig", "cow", "horse", "rabbits")
print(animal_b)
## [[1]]
## [1] "pig"
##
## [[2]]
## [1] "cow"
##
## [[3]]
## [1] "horse"
##
## [[4]]
## [1] "rabbits"
str(animal_b)
## List of 4
## $ : chr "pig"
## $ : chr "cow"
## $ : chr "horse"
## $ : chr "rabbits"
#List indexing
animal_b [2]
## [[1]]
## [1] "cow"
#dataframe
my_data <- data.frame(group = c("pig", "cow", "horse", "rabbits"),
value = c(5, 4, 2, 1))
print(my_data)
## group value
## 1 pig 5
## 2 cow 4
## 3 horse 2
## 4 rabbits 1
str(my_data)
## 'data.frame': 4 obs. of 2 variables:
## $ group: chr "pig" "cow" "horse" "rabbits"
## $ value: num 5 4 2 1
dim(my_data)
## [1] 4 2
View(my_data)
data_frame <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Score = c(95, 87, 74)
)
print(data_frame)
## Name Age Score
## 1 Alice 25 95
## 2 Bob 30 87
## 3 Charlie 35 74