Exercises
Session 1: Operators and basic operations
1- In a Value for Cultivation and Use (VCU) experiment in corn, the plots consist of two four-meter rows, spaced 0.60 m apart, totaling 6 plants per row. The plant heights were respectively (in meters): 1.45; 1.73; 1.98; 2.10; 2.03; 1.65; 2.15; 1.63; 1.53; 1.98; 2.05; 1.52.
- Create a height vector with the above values
- Calculate the average height
- Calculate the variance of heights
Answers:
To view the answer, just hover over the “Spoiler alert!”
b)1.816667
c)0.06540606
2- In the Genetics course, height and weight of 10 students were evaluated, 5 girls and 5 boys. See the table below with the respective values:
Name | Weight (kg) | Height (m) |
---|---|---|
Maria José | 72 | 1.65 |
Mariana | 57 | 1.68 |
Fernanda | 49 | 1.53 |
Jéssica | 62 | 1.70 |
Carolina | 57 | 1.52 |
Paulo | 90 | 1.82 |
Marcos | 53 | 1.63 |
João Paulo | 76 | 1.78 |
Geraldo | 63 | 1.72 |
Pedro | 92 | 1.98 |
- Calculate the BMI of each student, using the formula: \(BMI = \frac{weight}{height^2}\)
- Calculate the average BMI of the class
- Calculate the BMI variance
- Calculate the BMI standard deviation
Answers:
a)26.44628 20.19558 20.93212 21.45329 24.67105 27.17063 19.94806 23.98687 21.29529 23.46699
b)22.95662
c)6.665578
d)2.581778
3 - Solve the equation: \(2x^2 + 8x + 6 = 0\), using the quadratic formula. Remember that: \(x = \frac{-b\pm\sqrt{\Delta}}{2a}\); \(\Delta=b^2 - 4ac\)
Answers:
-3 -1
Session 2: Object Classes
1 - Load the dataset called “mtcars” (attach(mtcars)).
- Check the class of the “mtcars” object
- Check the class of columns “hp” and “cyl”
- Check the class of car names (rownames)
Answers:
a)“data.frame””
b)“numeric” “numeric”
c)“character”
2 - Using the same dataset from the previous exercise, calculate:
- The mean of the “hp” column
- Modify the class of the “hp” column to factor and calculate the mean again
- Compare the results obtained in letters a and b, and describe the differences between them
Answers:
a)146.6875
b)NA e um warning
3 - Create a vector from a Normal distribution sample with length 3000, mean 20, and standard deviation 5. Before creating the vector, use set.seed(123456).
- What is the sum of observations in the vector that are greater than 30?
- And what is the sum of observations less than the mean? NOTE: Calculate the mean of the set of values you just generated.
- Remove the smallest observation from the vector.
Answers:
a)2610.638
b)23976.86
c)only the “head”: 24.16867 18.61976 18.22499 20.43744 31.26128 24.17230
c)Minimum value: 1.170791
4 - In some experimental soybean areas, the presence of some nematodes was identified.
Area | Nematode | Area yield (kg/ha) | |
---|---|---|---|
A1 | Meloidogyne incognita | 2700 | |
A2 | Meloidogyne incognita | 2500 | |
A3 | Heterodera glycines | 2000 | |
A4 | NA | 3200 | |
A5 | Pratylenchus brachyurus | 2900 | |
A6 | NA | 3500 | |
A7 | Meloidogyne javanica | 2300 | |
A8 | NA | 3800 |
- Create a data frame with the information from the table above
- Create a column containing the municipality where each area is located (Piracicaba-SP, Rio Verde-GO, Londrina-PR, Barreiras-BA, Uberaba-MG, Rondonopolis-MT, Vilhena-RO, Gurupi-TO)
- What types of nematodes affect the evaluated areas?
- Which nematode had the highest impact on yield? And the lowest impact?
- Is the average yield of areas affected by Meloidogyne incognita higher than the average of areas affected by all other nematodes?
Answers:
c)“Heterodera glycines”, “Meloidogyne incognita”, “Meloidogyne javanica”, Pratylenchus brachyurus”
d)Highest impact: Heterodera glycines
Lowest impact: Pratylenchus brachyurus
e)Yes. The average yield of areas with M. incognita (2.6 kg/ha) is higher than the average of areas affected by others (2.4 kg/ha)
Session 3: Conditional and Loop Structures
1 - Create an object “a” containing a sequence from 1 to 5. Using any loop structure, add the number 8 to each element of “a”, until the sum of all elements is equal to or greater than 10000.
Answers:
2001 2002 2003 2004 2005
2 - Repeat the phrase “x sad tiger(s) ate x plates of wheat”, where x is an integer of your choice.
Answers:
If the chosen value was one: 1 sad tiger ate 1 plate of wheat.
If the chosen value was two: 1 sad tiger ate 1 plate of wheat. 2 sad tigers ate 2 plates of wheat.
3 - Time to relax! Create a loop to repeat a pattern from the famous Beatles song finale.
4 - Use the expression below to generate productivity means, considering 50 soybean genotypes, randomly:
Let’s assume that the national average soybean productivity is 3500.00 kg/ha. Using conditional structures, check if the genotypes show values above or below the national average productivity, returning the phrases “above average” or “below average”, respectively.
Answers:
[1] “above average” “below average” “below average” “above average” “above average” “above average” [7] “above average” “above average” “above average” “below average” “below average” “below average” [13] “below average” “above average” “above average” “above average” “below average” “above average” [19] “above average” “above average” “below average” “above average” “above average” “above average” [25] “above average” “below average” “above average” “above average” “above average” “below average” [31] “below average” “above average” “below average” “below average” “below average” “below average” [37] “above average” “above average” “above average” “below average” “below average” “above average” [43] “above average” “below average” “above average” “below average” “below average” “below average” [49] “above average” “below average”
5 - Using the following data:
temperatures <- c(25, 28, 32, 30, 24, 23, 26)
days <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
Create code to:
a)Classify temperatures (Hot:>=30; Warm: 25-29, Cool:>25)
b)Print daily report with “!” for hot days. A report like: * “Today is [day] and it’s [temperature]°C. It’s a [classification] day.” * “It’s hot today!” (if applicable)
c)Generate summary statistics saying which days were hot, which days are cool and the average temperature in warm days.
Answers:
[1] “Today is Mon and it’s 25°C. It’s a warm day.” [1] “Today is Tue and it’s 28°C. It’s a warm day.” [1] “Today is Wed and it’s 32°C. It’s a hot day.” [1] “Today is Thu and it’s 30°C. It’s a hot day.” [1] “Today is Fri and it’s 24°C. It’s a cool day.” [1] “Today is Sat and it’s 23°C. It’s a cool day.” [1] “Today is Sun and it’s 26°C. It’s a warm day.”
[1] “Today is Mon and it’s 25°C. It’s a warm day.” [1] “Today is Tue and it’s 28°C. It’s a warm day.” [1] “Today is Wed and it’s 32°C. It’s a hot day!” [1] “Today is Thu and it’s 30°C. It’s a hot day!” [1] “Today is Fri and it’s 24°C. It’s a cool day.” [1] “Today is Sat and it’s 23°C. It’s a cool day.” [1] “Today is Sun and it’s 26°C. It’s a warm day.”
Summary: Hot days:2 Cool days:Fri, Sat Average warm temp:26.3°C
Session 4: Functions
Use the dataset available below to generate productivity means, considering 25 soybean genotypes evaluated in 15 environments, randomly:
set.seed(123456)
data = data.frame(Genotypes = rep(seq(1,25,1), 15), Environments = rep(x = seq(1:15), each = 25), yield = rnorm(375, 3000:3500, 800))
Create a function to calculate the means and coefficient of variation (\(CV = \frac{\mu}{\sigma}*100\)) in each environment, and also evaluate the experimental precision (up to 20% = high; between 20 and 30% = medium; above 30% = low). The function should return the results containing the identification of each environment, the means, CVs, and precisions.
At this point in the course, you may have already dealt with various frustrations: the code doesn’t run, it took several attempts to get it right. You might feel like you’re falling into that learning progress graph we showed in the theoretical presentation. That’s why we’ve included some motivational videos here. These are three excellent TED talks (as TEDs usually are).
These two have subtitles in multiple languages. Just adjust the settings:
Teach girls bravery, not perfection | Reshma Saujani Special for women. Discusses how learning to code teaches us to better deal with our frustrations and diverts us from the imposed culture of perfection.
Mitch Resnick: Let’s teach kids to code The speaker is one of the developers of Scratch, which is a game that teaches programming logic. The speaker emphasizes the importance of learning a programming language in today’s world.
This one is in English only:
- You Should Learn to Program: Christian Genco at TEDxSMU Highlights the impacts of knowing how to program in daily life.
Extra: The apply
Family
1 - Use the function below to generate productivity means, considering 25 soybean genotypes evaluated in 15 environments, randomly:
set.seed(123456)
data = data.frame(Genotypes = rep(seq(1,25,1), 15), Environments = rep(x = seq(1:15), each = 25), yield = rnorm(375, 3000:3500, 800))
Using functions from the “apply” family, obtain the general means of each genotype and also of each environment.