Lec 13
Lec 13
•
NP
Creating Variables
a <- 1
a
## [1] 1
b <- 2
b
## [1] 2
c <- 3
c
## [1] 3
d <- 3.5
d
## [1] 3.5
NP
a+b
## [1] 3
a+b+c
## [1] 6
4/b
## [1] 2
5*b
## [1] 10
NP
c()
var1 <- c(1, 2, 5, 7, 8) # creating var1 with 5 values
var1
## [1] 1 2 5 7 8
## [1] 1 2 3 4 5
NP
seq()
var3 <- seq(1, 5) # creating var3 with a sequence from 1 to 5
var3
## [1] 1 2 3 4 5
var4 <- seq(1, 10, by = 2) # creating var4 with a sequence from 1 to 10 and with an interval of 2
var4
## [1] 1 3 5 7 9
var5 <- seq(1, 10, by = 3) # creating var5 with a sequence from 1 to 10 and with an interval of 3
var5
## [1] 1 4 7 10
NP
## [1] 1 2 5 7 8
var1+2
## [1] 3 4 7 9 10
var1
## [1] 1 2 5 7 8
var2
## [1] 1 2 3 4 5
var1+var2
## [1] 2 4 8 11 13
NP
## [1] "a"
## [1] "text"
## [1] 1 2 3
# Applying function
mean(x)
## [1] 2
max(x)
## [1] 3
min(x)
## [1] 1
NP
## [1] "Hello,World,is,good!"
NP
## [1] 2
Individual Exercise
Q3. Create and print a variable that contains the mean value
Create and print a variable that contains the mean test score. Apply the codes used from the previous
questions.
To the World of Data Frames!
NP
Data Frame
• ‘Column’ is an attribute
• ‘Row’ is the information of a person
NP
## [1] 90 80 60 70
math <- c(50, 60, 100, 20) # Creating math score variable
math
## [1] 50 60 100 20
# coding english and math to a data frame and assigning them to df_midterm
df_midterm <- data.frame(english, math)
df_midterm
## english math
## 1 90 50
## 2 80 60
## 3 60 100
## 4 70 20
NP
class <- c(1, 1, 2, 2)
class
## [1] 1 1 2 2
## [1] 75
## [1] 57.5
NP
Q1. Using c() and data.frame() together, create and print the following data frame.
fruit price volume
Apple 1800 24
Strawberry 1500 38
Watermelon 3000 13
Q2. Using the data frame created above, compute the means of the price and
volume.
NP
## # A tibble: 20 x 5
## id class math english science
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 50 98 50
## 2 2 1 60 97 60
## 3 3 1 45 86 78
## 4 4 1 30 98 58
## 5 5 2 25 80 65
## 6 6 2 50 89 98
## 7 7 2 80 90 45
## 8 8 2 90 78 25
## 9 9 3 20 98 15
## 10 10 3 50 98 45
## 11 11 3 65 65 65
## 12 12 3 45 85 32
## 13 13 4 46 98 65
## 14 14 4 48 87 12
## 15 15 4 75 56 78
## 16 16 4 58 98 65
## 17 17 5 65 68 98
## 18 18 5 80 78 90
## 19 19 5 89 68 87
## 20 20 5 78 83 58
mean(df_exam$english)
## [1] 84.9
mean(df_exam$science)
## [1] 59.45
NP
Loading RData
rm(df_midterm)
df_midterm
load("df_midterm.rda")
df_midterm
• Rda files, when loaded, automatically becomes the data frame without assignment.
Wrap-up
# 1.Creating variables and data frames
english <- c(90, 80, 60, 70) # Create ‘english’ variable
math <- c(50, 60, 100, 20) # Create ‘math’ variable
data.frame(english, math) # Create data frame
# Excel file
library(readxl) # Load readxl package
df_exam <- read_excel("excel_exam.xlsx") # Loading excel file
# CSV file
df_csv_exam <- read.csv("csv_exam.csv") # Loading csv file
write.csv(df_midterm, file = "df_midterm.csv") # Saving as a csv file
# Rda file
load("df_midterm.rda") # Loading Rda file
save(df_midterm, file = "df_midterm.rda") # Saveing as a Rda file