Exercise 1: Lesson2

Q1. What is the value of each object? Run the code and print the values.

mass <- 47.5            # mass?
age  <- 122             # age?
mass <- mass * 2.0      # mass?
age  <- age - 20        # age?
mass_index <- mass / age  # mass_index?

(Question taken from https://carpentries-incubator.github.io/bioc-intro/23-starting-with-r/index.html)

Q1: Solution
mass <- 47.5            # mass?
mass
## [1] 47.5
age  <- 122             # age?
age
## [1] 122
mass <- mass * 2.0      # mass?
mass
## [1] 95
age  <- age - 20        # age?
age
## [1] 102
mass_index <- mass / age  # mass_index?
mass_index
## [1] 0.9313725

Q2. Create the following objects; give each object an appropriate name.

a. Create an object that has the value of the number of bones in the adult human body.

b. We can create a vector of values using c(). For example to create a vector of fruits, we could use the following: fruit <- c("apples", "bananas", "mango", "kiwi"). Use this information to create an object containing the names of four different bones. (We will learn more about vectors in Lesson 3.)

Q2: Solution
# a.
bone_num<- 206 
bone_num
## [1] 206

# b. 
bone_names<- c("talus","calcaneus","tibia","fibula") 
bone_names
## [1] "talus"     "calcaneus" "tibia"     "fibula"

Q3. What types of data are stored in the objects created in question 2.

Q3: Solution
typeof(bone_num)
## [1] "double"
typeof(bone_names)
## [1] "character"

Q4. Modify bone_num to contain the number of bones in an adult human hand.

Q4: Solution
bone_num <- 27
bone_num
## [1] 27

Q5. Here is an object storing multiple values:

num_vec <- c(1:100)

What is the mean of this vector? How about the median? What functions can you use to find this information?

Q5: Solution
mean(num_vec)
## [1] 50.5
median(num_vec)
## [1] 50.5

Q6. What does the function paste() do? How can you find out? Can you use it to collapse bone_names into a string of length 1? Hint: Read the help documentation closely.

Q6: Solution
# To find help, use the ?
?paste

# To collapse the vector to length 1, check the collapse argument
paste(bone_names, collapse=", ")
## [1] "talus, calcaneus, tibia, fibula"
length(bone_names)
## [1] 4