This is my first class in R
# this is a comment
print("hello world")
## [1] "hello world"
fahrenheit_to_celsius <- function(temp_F) {
temp_C <- (temp_F - 32) * 5 / 9
return(temp_C)
}
# freezing point of water
fahrenheit_to_celsius(32)
## [1] 0
# boiling point of water
fahrenheit_to_celsius(212)
## [1] 100
you can write what you wish
# math
3 + 5
## [1] 8
12 / 7
## [1] 1.714286
weight_kg <- 55
weight_kg
## [1] 55
(weight_kg)
## [1] 55
2.2 * weight_kg
## [1] 121
weight_kg <- 57.5
2.2 * weight_kg
## [1] 126.5
weight_1b <- 2.2 * weight_kg
weight_kg <- 100
weight_kg <- sqrt(10)
round(3.14159)
## [1] 3
args(round)
## function (x, digits = 0, ...)
## NULL
round(3.14159)
## [1] 3
args(round)
## function (x, digits = 0, ...)
## NULL
?round
## starting httpd help server ... done
round(digits = 2, x = 3.14159)
## [1] 3.14
round(3.14159, digits = 2)
## [1] 3.14
getwd()
## [1] "C:/Users/User/Desktop/S's Digital lab/courseware yr 3/0310 bioinfo/Tutorial4Project1"
?mean
??kruskall
fahrenheit_to_celsius <- function(temp_F) {
temp_C <- (temp_F - 32) * 5 / 9
return(temp_C)
}
weight_g <- c(50, 60, 65, 82)
animals <- c("mouse", "rat", "dog")
length(weight_g)
## [1] 4
length(animals)
## [1] 3
class(weight_g)
## [1] "numeric"
class(animals)
## [1] "character"
str(weight_g)
## num [1:4] 50 60 65 82
str(animals)
## chr [1:3] "mouse" "rat" "dog"
weight_g <- c(weight_g, 90) # add to the end of the vector
weight_g <- c(30, weight_g) # add to the beginning of the vector
weight_g
## [1] 30 50 60 65 82 90
typeof(animals)
## [1] "character"
This is the first challenge of the tutorial
num_char <- c(1, 2, 3, "a")
num_logical <- c(1, 2, 3, TRUE)
char_logical <- c("a", "b", "c", TRUE)
tricky <- c(1, 2, 3, "4")
number>logical character>number character>logical So character>number>logical
class(num_char)
## [1] "character"
typeof(num_char)
## [1] "character"
class(num_logical)
## [1] "numeric"
typeof(num_logical)
## [1] "double"
class(char_logical)
## [1] "character"
typeof(char_logical)
## [1] "character"
class(tricky)
## [1] "character"
typeof(tricky)
## [1] "character"
combined_logical <- c(num_logical, char_logical)
animals = c(animals, "cat")
animals[2]
## [1] "rat"
animals[c(3, 2)]
## [1] "dog" "rat"
(more_animals <- animals[c(1, 2, 3, 2, 1, 4)])
## [1] "mouse" "rat" "dog" "rat" "mouse" "cat"
weight_g <- c(21, 34, 39, 54, 55)
weight_g[c(TRUE, FALSE, FALSE, TRUE, TRUE)]
## [1] 21 54 55
weight_g > 50 # will return logicals with TRUE for the indices that meet the condition
## [1] FALSE FALSE FALSE TRUE TRUE
## so we can use this to select only the values above 50
weight_g[weight_g > 50]
## [1] 54 55
weight_g[weight_g > 30 & weight_g < 50]
## [1] 34 39
weight_g[weight_g <= 30 | weight_g == 55]
## [1] 21 55
weight_g[weight_g >= 30 & weight_g == 21]
## numeric(0)
animals <- c("mouse", "rat", "dog", "cat", "cat")
# return both rat and cat
animals[animals == "cat" | animals == "rat"]
## [1] "rat" "cat" "cat"
# return a logical vector that is TRUE for the elements within animals
# that are found in the character vector and FALSE for those that are not
animals %in% c("rat", "cat", "dog", "duck", "goat", "bird", "fish")
## [1] FALSE TRUE TRUE TRUE TRUE
# use the logical vector created by %in% to return elements from animals
# that are found in the character vector
animals[animals %in% c("rat", "cat", "dog", "duck", "goat", "bird", "fish")]
## [1] "rat" "dog" "cat" "cat"
"four">"five"
## [1] TRUE
"cat">"dog"
## [1] FALSE
"aa">"ab"
## [1] FALSE
"ab">"aa"
## [1] TRUE
heights <- c(2, 4, 4, NA, 6)
mean(heights)
## [1] NA
max(heights)
## [1] NA
mean(heights, na.rm = TRUE)
## [1] 4
max(heights, na.rm = TRUE)
## [1] 6
## Extract those elements which are not missing values.
heights[!is.na(heights)]
## [1] 2 4 4 6
## Returns the object with incomplete cases removed.
#The returned object is an atomic vector of type `"numeric"` (or #`"double"`).
na.omit(heights)
## [1] 2 4 4 6
## attr(,"na.action")
## [1] 4
## attr(,"class")
## [1] "omit"
## Extract those elements which are complete cases.
#The returned object is an atomic vector of type `"numeric"` (or #`"double"`).
heights[complete.cases(heights)]
## [1] 2 4 4 6
#Challenge 1.
heights <- c(63, 69, 60, 65, NA, 68, 61, 70, 61, 59, 64, 69, 63, 63, NA, 72, 65, 64, 70, 63, 65)
(heights_no_na <- heights[!is.na(heights)])
## [1] 63 69 60 65 68 61 70 61 59 64 69 63 63 72 65 64 70 63 65
median(heights_no_na)
## [1] 64
length(heights_no_na[heights_no_na > 67])
## [1] 6