Write R code to generate the expected output shown below each
question.
1. Create two variables ‘x’ and ‘y’ and assign values 3 and 6. Print
the value of x+y
## [1] 9
2. Create a variable named ‘welcome’ and assign the text “Welcome to
R!” to it. Print the variable.
## [1] "Welcome to R!"
3. Create a vector named ‘rainfall’ with numbers 16, 18, 14, 22, 27,
17, 19, 17, 17, 22, 20, and 22. Print the vector.
## [1] 16 18 14 22 27 17 19 17 17 22 20 22
4. Calculate the mean rainfall
## [1] 19.25
5. Values in the vector ‘rainfall’ are in inches. You can calculate
the rainfall in centimeters by dividing each value by 0.3937. Write a
‘for’ loop to iterate over each value in the rainfall vector and print
the rainfall in centimeters
## [1] 40.64
## [1] 45.72
## [1] 35.56
## [1] 55.88
## [1] 68.58
## [1] 43.18
## [1] 48.26
## [1] 43.18
## [1] 43.18
## [1] 55.88
## [1] 50.8
## [1] 55.88
6. Use a ‘for’ loop and an ‘if-else’ condition to print the values
only if the rainfall is greater than 50 centimerters.
## [1] 55.88
## [1] 68.58
## [1] 55.88
## [1] 50.8
## [1] 55.88
7. Create a function to covert rainfall in inches to centimeters.
Round the answer to 2 decimal places. Pass the value 24 to the function
and print the output with “cm” at the end.
## [1] "60.96 cm"