-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW3.R
52 lines (24 loc) · 1.54 KB
/
W3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Week 3: Loops, Conditional Statements, and Functions Assignment
# Exercise 1: For Loops
# Write a for loop that prints the numbers from 1 to 10.
# Create a vector of numbers c(2, 4, 6, 8, 10). Write a for loop to multiply each number by 3 and print the result.
# Exercise 2: While Loops
# Write a while loop that keeps printing a number starting from 5, subtracting 1 each time until the number reaches 0.
# Modify the loop to break when the number reaches 2.
# Exercise 3: Conditional Statements
# Create a variable x <- 15.
# Write an if statement to print "x is greater than 10" if the condition holds.
# Expand it to an if-else statement to handle the case when x <= 10.
# Use an ifelse() function on a vector c(3, 7, 12, 18, 5) to return "Pass" for elements greater than 10 and "Fail" otherwise.
# Exercise 4: Creating Functions
# Write a function square_num() that takes a number as input and returns its square.
# Test your function on the numbers 4, 7, and 9.
# Write a more complex function that takes two arguments x and y, adds them together, and multiplies by 5.
# Exercise 5: Apply Functions
# Create a matrix matrix_1 <- matrix(1:9, nrow = 3).
# Use apply() to calculate the sum of each row.
# Use lapply() on a list containing 1:3, 4:6, and 7:9 to find the mean of each element.
# Show & Tell Bonus: Try Something New
# Explore a new topic related to loops, conditionals, or functions in R.
# Share your R code and explain what you discovered.
# Why did you found it useful.