224 + 667
## [1] 891
35/7
## [1] 5
(22*5)+3
## [1] 113
The following code chunk calculates exam average scores for seven subjects one by one
(92 + 87 + 85)/3 #Math
## [1] 88
(90 + 81 + 92)/3 #Chemistry
## [1] 87.66667
(84 + 95 + 79)/3 #Writing
## [1] 86
(95 + 86 + 79)/3 #Art
## [1] 86.66667
(77 + 85 + 90)/3 #History
## [1] 84
(92 + 90 + 91)/3 #Music
## [1] 91
(85 + 88 + 95)/3 #Physical_Education
## [1] 89.33333
Creating varibles for storing average scores for the subjects
math <- (92 + 87 + 85)/3
math
## [1] 88
Chemistry <- (90 + 81 + 92)/3
Chemistry
## [1] 87.66667
Writing <- (84 + 95 + 79)/3
Writing
## [1] 86
Art <- (95 + 86 + 93)/3
Art
## [1] 91.33333
History <- (77 + 85 + 90)/3
History
## [1] 84
Music <- (92 + 90 + 91)/3
Music
## [1] 91
Physical_Education <- (85 + 88 + 95)/3
Physical_Education
## [1] 89.33333
average_math_and_chem <- (math + Chemistry)/2
average_math_and_chem
## [1] 87.83333
gpa <- (math + Chemistry + Writing + Art + History + Music + Physical_Education)/7
gpa
## [1] 88.19048
Vectors -> Storage objects that can hold multiple values Storing values on vectors enables you to perfom operations on all of them at once c() is used to create a vector
math_chem <- c(math,Chemistry)
math_chem
## [1] 88.00000 87.66667
final_scores <- c(math, Chemistry, Writing, Art , History , Music , Physical_Education)
final_scores
## [1] 88.00000 87.66667 86.00000 91.33333 84.00000 91.00000 89.33333
mean() FuctionThe input to mean() is a vector, and the output is the average of the values contained in the vector.
mean (math_chem)
## [1] 87.83333
mean (final_scores)
## [1] 88.19048
min(): Takes a vector as input, output is the smallest value in the vector.max(): Takes a vector as input, output is the largest value in the vector.length(): Takes a vector as input, output is the total number of values in the vector.sum(): Takes a vector as input, output is the sum of all values in the vector.min(math_chem)
## [1] 87.66667
min(final_scores)
## [1] 84
max(final_scores)
## [1] 91.33333
length(final_scores)
## [1] 7
sum(final_scores)
## [1] 617.3333
Extracting a value
final_scores[2]
## [1] 87.66667
stem_grades <- final_scores[1:2]
stem_grades
## [1] 88.00000 87.66667
# non_stem_grades <- final_scores[c(3,4,5,6,7)]
non_stem_grades <- final_scores [3:7]
non_stem_grades
## [1] 86.00000 91.33333 84.00000 91.00000 89.33333
using the property to calcute mean for stem and non stem subjects
mean(stem_grades)
## [1] 87.83333
mean(non_stem_grades)
## [1] 88.33333
names() functionmath_chemistry <- c(88.00000 , 87.66667)
math_chemistry
## [1] 88.00000 87.66667
class_names <- c('math', 'chemistry')
class_names
## [1] "math" "chemistry"
names(math_chemistry) <- class_names
math_chemistry
## math chemistry
## 88.00000 87.66667
class_names <- c('math','chemistry','writing','Art','history','music','physical_education')
class_names
## [1] "math" "chemistry" "writing"
## [4] "Art" "history" "music"
## [7] "physical_education"
final_scores <- c(88, 87.66667, 86, 91.33333, 84, 91, 89.33333)
final_scores
## [1] 88.00000 87.66667 86.00000 91.33333 84.00000 91.00000 89.33333
names(final_scores) <- class_names
final_scores
## math chemistry writing
## 88.00000 87.66667 86.00000
## Art history music
## 91.33333 84.00000 91.00000
## physical_education
## 89.33333
math_chemistry['chemistry']
## chemistry
## 87.66667
math_chemistry[1]
## math
## 88
math_chemistry[3]
## <NA>
## NA
c():liberal_arts <- final_scores [c('writing','history')]
liberal_arts
## writing history
## 86 84
fine_arts <- final_scores [c('Art','music')]
fine_arts
## Art music
## 91.33333 91.00000
mean(fine_arts)
## [1] 91.16666
mean(liberal_arts)
## [1] 85
math_comparison <- final_scores['math'] > final_scores
math_comparison
## math chemistry writing
## FALSE TRUE TRUE
## Art history music
## FALSE TRUE FALSE
## physical_education
## FALSE
typeof(math_comparison)
## [1] "logical"
gpa <- mean (final_scores)
gpa
## [1] 88.19048
above_average <- final_scores > gpa
above_average
## math chemistry writing
## FALSE FALSE FALSE
## Art history music
## TRUE FALSE TRUE
## physical_education
## TRUE
best_grades <- final_scores[above_average]
best_grades
## Art music physical_education
## 91.33333 91.00000 89.33333
Johhny Results - Subjects : math, chemistry, writing, art, history, music, physical_education
tests <- c(76, 89, 78, 88, 79, 93, 89)
homework <- c(85, 90, 88, 79, 88, 95, 74)
projects <- c(77, 93, 87, 90, 77, 82, 80)
math <- (76 + 85 + 77)/3
math
## [1] 79.33333
chemistry <- (89 + 90 +93)/3
chemistry
## [1] 90.66667
writing <- (78 + 88 + 87)/3
writing
## [1] 84.33333
art <- (88 + 79 + 90)/3
art
## [1] 85.66667
history <- (79 + 88 + 77)/3
history
## [1] 81.33333
music <- (93 + 95 + 82)/3
music
## [1] 90
physical_education <- (89 + 74 + 80)/3
physical_education
## [1] 81
-This is a simplified method of doing the above calculations.
johnny_scores <-(tests + homework + projects)/3
johnny_scores
## [1] 79.33333 90.66667 84.33333 85.66667 81.33333 90.00000 81.00000
mean(johnny_scores)
## [1] 84.61905
scores <- c(76 , 81 ,92, 56)
scores_names <- c("Programming","Statistics","Pharmacy","Language")
names(scores) <- scores_names
scores
## Programming Statistics Pharmacy Language
## 76 81 92 56
tests <- c (76, 89, 78)
homework <- c (85 , 90 ,88, 79, 88, 95, 74)
projects <- c (77, 93, 87 ,77 , 82, 80)
kate_results <- tests + homework + projects
kate_results
## [1] 238 272 253 232 259 253 227
#Adding Missing Tests to Kate's results
tests <- c(tests,88,79,93,89)
tests
## [1] 76 89 78 88 79 93 89
length(tests)
## [1] 7
# deleting elements from a vector
tests_del <- tests[-c(8:43)]
tests_del
## [1] 76 89 78 88 79 93 89
tests
## [1] 76 89 78 88 79 93 89
Kate has asked you to help her figure out which of her classes were her weakest so that she can improve
class_names <- c("math", "chemistry", "writing", "art", "history", "music", "physical_education")
kate_grades <- (tests + homework + projects)/3
## Warning in tests + homework + projects: longer object length is not a
## multiple of shorter object length
kate_grades
## [1] 79.33333 90.66667 84.33333 81.33333 83.00000 89.33333 80.00000
names (kate_grades) <- class_names
kate_grades
## math chemistry writing
## 79.33333 90.66667 84.33333
## art history music
## 81.33333 83.00000 89.33333
## physical_education
## 80.00000