<- c(
species "MountainBeaver", "Cow", "GreyWolf", "Goat",
"GuineaPig", "Diplodocus", "AsianElephant", "Donkey",
"Horse", "PotarMonkey", "Cat", "Giraffe",
"Gorilla", "Human", "AfricanElephant", "Triceratops",
"RhesusMonkey", "Kangaroo", "GoldenHamster", "Mouse",
"Rabbit", "Sheep", "Jaguar", "Chimpanzee",
"Rat", "Brachiosaurus", "Mole", "Pig"
)
<- c(
bodywt_kg 1.4, 465, 36.3, 27.7, 1., 11700, 2547, 187.1,
521, 10, 3.3, 529, 207, 62, 6654, 9400,
6.8, 35, 0.1, 0.02, 2.5, 55.5, 100, 52.2,
0.3, 87000, 0.1, 192
)
<- c(
brainwt_kg 0.0081, 0.423, 0.1195, 0.115, 0.0055, 0.05,
4.603, 0.419, 0.655, 0.115, 0.0256, 0.68,
0.406, 1.32, 5.712, 0.07, 0.179, 0.056,
0.001, 0.0004, 0.0121, 0.175, 0.157, 0.44,
0.0019, 0.1545, 0.003, 0.18
)
Solution to working with vectors
You have the following three vectors:
- Check which of the following animals are contained in the
species
vector
<- c("Snail", "Goat", "Chimpanzee", "Rat", "Dragon", "Eagle")
animals_to_check
# Goes through every element in animals_to_check and returns TRUE
# if it appears in species
%in% species animals_to_check
[1] FALSE TRUE TRUE TRUE FALSE FALSE
You can see that Goat, Chimpanzee and Rat are contained in species
.
To see the actual species name instead of just TRUE
and FALSE
, you have to index the vector:
%in% species] animals_to_check[animals_to_check
[1] "Goat" "Chimpanzee" "Rat"
Note
If you use %in%
to check if elements are contained in a vector, the result vector is of the same length as the vector that is before the %in%
.
Compare the result from above with the following:
# Goes through every element in species and returns TRUE
# if it appears in animals_to_check
%in% animals_to_check species
[1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
[25] TRUE FALSE FALSE FALSE
- Calculate mean and standard deviation of brain weight
mean(brainwt_kg)
[1] 0.5745214
sd(brainwt_kg)
[1] 1.334929
- Which species have a brain weight larger than the mean brain weight of all species?
> mean(brainwt_kg)] species[brainwt_kg
[1] "AsianElephant" "Horse" "Giraffe" "Human"
[5] "AfricanElephant"
- Calculate the ratio brain weight / body weight in percent and save it in a vector
<- brainwt_kg / bodywt_kg * 100 brain_body_ratio
- Are there any animals with a larger brain to body weight ratio than humans? If yes, which ones?
Step 1: Look at the brain to body ratio of humans
== "Human"] brain_body_ratio[species
[1] 2.129032
Step 2: Compare it with the other animals
# New variable for human brain to body ratio
<- brain_body_ratio[species == "Human"]
bbr_human # Are there animals that have a larger brain to body ratio than humans?
> bbr_human brain_body_ratio
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE TRUE FALSE
# Which are these animals
> bbr_human] species[brain_body_ratio
[1] "RhesusMonkey" "Mole"
# or short
> brain_body_ratio[species == "Human"]] species[brain_body_ratio
[1] "RhesusMonkey" "Mole"
1 Extras
- Round the vector to 4 decimal places:
<- round(brain_body_ratio, digits = 4)
brain_body_ratio brain_body_ratio
[1] 0.5786 0.0910 0.3292 0.4152 0.5500 0.0004 0.1807 0.2239 0.1257 1.1500
[11] 0.7758 0.1285 0.1961 2.1290 0.0858 0.0007 2.6324 0.1600 1.0000 2.0000
[21] 0.4840 0.3153 0.1570 0.8429 0.6333 0.0002 3.0000 0.0938
- Which animal has the smallest brain to body ratio?
== min(brain_body_ratio)] species[brain_body_ratio
[1] "Brachiosaurus"
The Brachiosaurus has a really small brain with a weight of just 0.1545 kg at a body weight of 8.7^{4} kg!
- Add elements to the vector
<- c("Eagle", "Snail", "Lion")
species_new <- c(0.0004, NA, 0.5)
brainwt_kg_new <- c(18, 0.01, 550)
bodywt_kg_new
<- c(species, species_new)
species <- c(brainwt_kg, brainwt_kg_new)
brainwt_kg <- c(bodywt_kg, bodywt_kg_new) bodywt_kg
- What is the mean brain weight now?
mean(brainwt_kg)
[1] NA
# na.rm = TRUE removes missing values before calculating the mean
mean(brainwt_kg, na.rm = TRUE)
[1] 0.5529