Introduction
Verify that R and RStudio are downloaded following Lecture 1.
Open RStudio.
In the console enter separately each of the following command lines:
1+1
[1] 2
die <- 1:6
die + die
[1] 2 4 6 8 10 12
Exercise 1
- Choose any number and add 2 to it.
- Multiply the result by 3.
- Subtract 6 from the answer.
- Divide what you get by 3.
# We choose the number 101
a <- 101 + 2
b <- a * 3
c <- b - 6
d <- c / 3
print(d)
[1] 101
Exercise 2
Define the function “magic” that takes a number as an argument and
repeat the steps from Exercise 1. Set “10” as the default value.
magic <- function(x){
y <- x + 2
z <- y*3
w <- z - 6
v <- w/3
v
}
Call the function “magic” with different values passed as
arguments.
magic(10)
magic(2)
magic(7)
magic(1053)
Exercise 3
Write a function named “simple_math” that takes two numbers as input
and returns both their sum and their product. Include default values for
both arguments.
simple_math <- function(x = 101, y = 1024) {
sum_result <- x+y
product_result <- x*y
return(list(sum = sum_result, product = product_result))
}
Test your function
result <- simple_math()
print(result)
result <- simple_math(3, 4)
print(result)
Exercise 4
Let x3 be the following vector
x3 <- c(0, 1, 1, 2, 2, 2, 3, 3, 4)
Imagine what a histogram of x3 would look like. Assume that the
histogram has a bin width of 1. How many bars will the histogram have?
Where will they appear? How high will each be?
When you are done, plot a histogram of x3 with binwidth = 1, and see
if you are right.
# You can make a histogram of x3 with qplot(x3, binwidth = 1). The histogram will look like a symmetric pyramid. The middle bar will have a height of 3 but be sure to try it and see for yourself.
# You can use a histogram to display visually how common different values of x are. Numbers covered by a tall bar are more common than numbers covered by a short bar.
Exercise 5
Step 1
The ggplot2 package is a powerful tool for creating a wide range of
plots and visualizations in R. 1. Install the ggplot2 package using the
following command:
install.packages("ggplot2")
Confirm that the package has been installed by checking the output in
your console.
Step 2
After installing the package, load it into your R session:
library(ggplot2)
Step 3
Let’s create a scatter plot to visualize the relationship between two
sets of numbers.
- Create two vectors of numbers:
x_values <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y_values <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
- Use ggplot2 to create a scatter plot directly from these
vectors:
ggplot() +
geom_point(aes(x = x_values, y = y_values)) +
labs(title = "Scatter Plot of x vs. y",
x = "X Values",
y = "Y Values")
Explanations
The ggplot() function initializes the plotting environment.
geom_point(aes(x = x_values, y = y_values)) creates a scatter plot with
points defined by x_values and y_values.
labs() adds labels and a title to your plot.
LS0tCnRpdGxlOiAiU1RTQ0kgMjEyMDogRGlzY3Vzc2lvbiAxIgphdXRob3I6ICJOYXllbCBCZXR0YWNoZSIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKIyBJbnRyb2R1Y3Rpb24KClZlcmlmeSB0aGF0IFIgYW5kIFJTdHVkaW8gYXJlIGRvd25sb2FkZWQgZm9sbG93aW5nIFtMZWN0dXJlIDFdKGh0dHBzOi8vbmF5ZWxiZXR0YWNoZS5naXRodWIuaW8vZG9jdW1lbnRzL1NUU0NJXzIxMjAvbGVjXzEucGRmICJMaW5rIHRvIExlY3R1cmUgMSIpLlwKT3BlbiBSU3R1ZGlvLlwKSW4gdGhlIGNvbnNvbGUgZW50ZXIgc2VwYXJhdGVseSBlYWNoIG9mIHRoZSBmb2xsb3dpbmcgY29tbWFuZCBsaW5lczoKCmBgYHtyfQoxKzEKYGBgCgpgYGB7cn0KZGllIDwtIDE6NgpgYGAKCmBgYHtyfQpkaWUgKyBkaWUKYGBgCgojIEV4ZXJjaXNlIDEKCjEuICBDaG9vc2UgYW55IG51bWJlciBhbmQgYWRkIDIgdG8gaXQuXAoyLiAgTXVsdGlwbHkgdGhlIHJlc3VsdCBieSAzLlwKMy4gIFN1YnRyYWN0IDYgZnJvbSB0aGUgYW5zd2VyLlwKNC4gIERpdmlkZSB3aGF0IHlvdSBnZXQgYnkgMy5cCgpgYGB7cn0KIyBXZSBjaG9vc2UgdGhlIG51bWJlciAxMDEKYSA8LSAxMDEgKyAyCmIgPC0gYSAqIDMKYyA8LSBiIC0gNgpkIDwtIGMgLyAzCnByaW50KGQpCmBgYAoKIyBFeGVyY2lzZSAyCgpEZWZp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