## [1] 80 65 90 70
## attr(,"na.action")
## [1] 3 5
## attr(,"class")
## [1] "omit"
test_scores <- c(80, 65, NA, 90, NA, 70)
test_scores_without_NAs = na.omit(test_scores)
test_scores_without_NAs
## $captain
## [1] "Kirk"
##
## $<NA>
## [1] "Janeway"
##
## $<NA>
## [1] "Sisko"
star_trek_df = data.frame(captain = c("Kirk", "Janeway", "Sisko"),
starship = c("Enterprise", "Voyager", "Defiant"))
captains_lst = as.list(star_trek_df$captain)
names(captains_lst) = "captain"
captains_lst
## [1] "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"
## [7] "Saturday"
days_of_week = c("Friday", "Monday", "Tuesday", "Thursday", "Saturday", "Sunday", "Wednesday")
days_of_week_fct = factor(days_of_week, levels =c("Sunday",
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
))
levels(days_of_week_fct)
## [1] "There are outliers"
## [1] "There are outliers"
test_scores = c(64, 80, 95, 42, 75)
outlier = ifelse(any(test_scores < 50 | test_scores > 95), "There are outliers", "There are no outliers")
outlier
## Test score total is 369
test_scores = c(64, 80, 55, 95, 102, 75)
test_scores_total = 0
for(score in test_scores) {
if(score<100)
test_scores_total = test_scores_total + score
}
cat("Test score total is", test_scores_total)
## [1] 45 55 60 75 76 82 85
## [1] 45 45 55 60 75 76 82 85
## [1] 45 45 45 55 60 75 76 82 85
## [1] 60 75 76 82
## [1] 60 75
## [1] 60 75 76
test_scores = c(75, 90, 45, NA, 60, 45, 85, 96, NA, 45, 55, 76, 82)
remove_NA_lowest_highest = function(x, y, z){
x = sort(x)
n = length(x)
quant = quantile(x, probs=c(.25, .75), na.rm = TRUE)
rm.lowest.n= head(x[c(x<quant[[1]])],2)
rm.highest.n = head(x[c(x>quant[[2]])],2)
if (y < 0 | z< 0) {
stop("rm arguments must be greater than or equal to zero ")
}
else if((y +z) >= n) {
stop("Sum of rm arguments greater than or equal to zero ")}
result = x[(y+1):(n-z)]
}
print(remove_NA_lowest_highest(test_scores, 2, 2))
print(remove_NA_lowest_highest(test_scores, 1, 2))
print(remove_NA_lowest_highest(test_scores, 0, 2))
print(remove_NA_lowest_highest(test_scores, 4, 3))
print(remove_NA_lowest_highest(test_scores, 4, 5))
print(remove_NA_lowest_highest(test_scores, 4, 4))
print(remove_NA_lowest_highest(test_scores, -1, 1))
print(remove_NA_lowest_highest(test_scores, 6,5))
## List of 3
## $ numbers: num [1:5] 9 7 4 3 2
## $ words : chr [1:5] "nine" "seven" "four" "three" ...
## $ even : chr [1:5] "FALSE" "FALSE" "TRUE" "FALSE" ...
vec_1 = c(7, NA, 9, 4, NA, 2, 3, 2)
problem_3_lst = list("numbers" = rev(sort(unique((vec_1)))), "words" =numbers_to_words(rev(sort(unique((vec_1))))), "even" = ifelse((rev(sort(unique((vec_1)))) %% 2) == 0, "TRUE", "FALSE")
)
str(problem_3_lst)
## $var_1
## [1] 64 26 21 14 NA 6 6 NA 16 5
##
## $var_2
## [1] 64 26 21 14 0 6 6 0 16 5
##
## $var_3
## [1] 64 26
##
## $var_4
## [1] 15.8
original_vector = c(64, 26, 21, 14, NA, 6, 6, NA, 16, 5)
problem_4_lst = list("var_1" = as.integer(original_vector),"var_2" = as.integer(replace(original_vector, is.na(original_vector), 0)), "var_3" = as.double(replace(original_vector, is.na(original_vector), 0) [(replace(original_vector, is.na(original_vector), 0)>24)]), "var_4" = mean(as.integer(replace(original_vector, is.na(original_vector), 0))))
problem_4_lst
## [1] "Common Ostrich" "Somali Ostrich" "Common/Somali Ostrich"
## [4] "Greater Rhea" "Lesser Rhea" "Lesser Rhea (Puna)"
## [1] "Bar-headed Goose"
## [1] "Choco Tinamou"
## [2] "Garganey x Northern Shoveler (hybrid)"
## [3] "Greater White-fronted x Barnacle Goose (hybrid)"
## [4] "Masked Duck"
## [5] "Mauritius Duck"
## [6] "Musk Duck"
## [7] "Ruddy Duck"
## [8] "Tundra Swan (Whistling)"
#sessionInfo()
#class(ebird_taxonomy)
#str(ebird_taxonomy)
data = ebird_taxonomy
my_ebird_taxonomy = head(ebird_taxonomy$common_name, 500)
head(my_ebird_taxonomy)
my_100th_bird = my_ebird_taxonomy[100]
my_100th_bird
set.seed(3)
my_ebird_sample = sort(sample(my_ebird_taxonomy, 8, replace = FALSE))
my_ebird_sample
## [1] 65.6
## [1] 1.3
## [1] 100.0 39.0 91.0 39.0 75.4 88.4 100.0 54.6
## orig_scores final_scores
## 1 90 100.0
## 2 30 39.0
## 3 70 91.0
## 4 30 39.0
## 5 58 75.4
## 6 68 88.4
## 7 95 100.0
## 8 42 54.6
## [1] "A" "F" "A" "F" "C" "B" "A" "F"
## [1] "A" "F" "A" "F" "C" "B" "A" "F"
## [1] "A" "F" "A" "F" "C" "B" "A" "F"
original_scores = c(90, 30, 70, 30, 58, 68, 95, 42)
original_scores_wo_min = original_scores[!(original_scores ==
min(original_scores))]
original_scores_wo_min_max =
original_scores_wo_min[!(original_scores_wo_min ==
max(original_scores_wo_min))]
revised_scores = original_scores_wo_min_max
revised_scores_mean = mean(revised_scores)
revised_scores_mean
score_adjustment_multiplier = ifelse(revised_scores_mean< 50, 1.4,
ifelse(revised_scores_mean>=50 & revised_scores_mean<70, 1.3, 1.2))
score_adjustment_multiplier
adjusted_scores = original_scores * score_adjustment_multiplier
adjusted_scores = replace(adjusted_scores, adjusted_scores > 100, 100)
adjusted_scores
scores_df = rbind(original_scores, adjusted_scores)
scores_df = as.data.frame(t(scores_df))
colnames(scores_df) = c("orig_scores", "final_scores")
scores_df
letter_grades = c("F", "D", "C", "B", "A")
break_pts = c(-Inf, 59.995, 69.995, 79.995, 89.995, Inf)
get_letter_grade = function(x){
letter_grade= ifelse(x < 70, "F",
ifelse( x >= 70 & x < 80, "C",
ifelse(x>=80 & x <90, "B", "A")))
print(letter_grade)
}
get_letter_grade(scores_df$final_scores)
scores_df$grades = get_letter_grade(scores_df$final_scores)
scores_df$grades
##
## Asia Europe North America
## 1 1 2
city_df = data.frame(name = c("London", "New York", "Beijing","Ottawa"),
capital = c(TRUE, FALSE, TRUE, TRUE),
continent = c("Europe", "North America", "Asia", "North America" ))
continent_count = table(city_df$continent)
continent_count