0% found this document useful (0 votes)
2 views3 pages

2.18 Problem Set

The document is a problem set that guides the reader through various tasks in R programming, including creating variables of different data types, performing mathematical operations, and manipulating vectors. It also includes exercises on loops and conditional statements. Each task is accompanied by example code to illustrate the concepts being taught.

Uploaded by

Trinh Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

2.18 Problem Set

The document is a problem set that guides the reader through various tasks in R programming, including creating variables of different data types, performing mathematical operations, and manipulating vectors. It also includes exercises on loops and conditional statements. Each task is accompanied by example code to illustrate the concepts being taught.

Uploaded by

Trinh Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

---

title: '2.18 Problem Set'


author: Trinh Le
---

1. Create three variables, numeric_variable, character_variable, and logical_variable.


a.Store a value of the corresponding data type in each variable. (Numeric value in
numeric_variable, etc.)
b.Use the correct function to return the data type of each variable.
c.Reassign new values to each variable, with the correct data types, and use the correct
function to return the data type of each variable.
```{r}
numeric_variable <- 42
character_variable <- "Hello"
logical_variable <- TRUE

print(class(numeric_variable))
print(class(character_variable))
print(class(logical_variable))

numeric_variable <- 3.14


character_variable <- "World"
logical_variable <- FALSE

print(class(numeric_variable))
print(class(character_variable))
print(class(logical_variable))
```

2. Create two numeric variables.


a.Perform a mathematical operation using the two variables and store the result in a third
variable. Print the value of the third variable.
b.Reassign new values to the two original variables. Create a test of equality or
inequality and store the result in a new variable.
c.Print the data types of the new variable from part a and the new variable from part b.
Add these two variables together. Does this work? If so, why? If not, why not?
```{r}
number1 <- 10
number2 <- 5
result <- number1 + number2
print(result)

number1 <- 20
number2 <- 15
test_result <- number1 != number2
print(test_result)

print(class(result))
print(class(test_result))
sum_result <- result + test_result
print(sum_result)

#It works. In R, we can add a number and a logical value (like TRUE or FALSE) because R
automatically changes TRUE to the number 1 and FALSE to the number 0. Therfore, when I add
15 (a number) and TRUE (which becomes 1), it works.
```

3. Create a numeric vector five elements long and store it in a variable.


a.Update the variable to lengthen the vector by three elements.
b.Select all elements that are greater than four.
c.Create a new variable and store the six middle elements in it.
d.Print both variables.
e.Combine the two vectors. Print the results but do not store them.
```{r}
numeric_vector <- c(1, 3, 5, 7, 9)
print(numeric_vector)

numeric_vector <- c(numeric_vector, 11, 13, 15)


print(numeric_vector)

greater_than_four <- numeric_vector[numeric_vector > 4]


print(greater_than_four)

middle_elements <- numeric_vector[2:7]


print(middle_elements)

print(numeric_vector)
print(middle_elements)

print(c(numeric_vector, middle_elements))
```

4. Create a vector that contains the five city names, including your hometown.
a.Select the first through third elements of the vector.
b.Select the element that is your hometown without using the index number.
```{r}
cities <- c("Seattle", "San Francisco", "Chicago", "New York", "Atlanta")
first_three_cities <- cities[1:3]
print(first_three_cities)
hometown <- cities[cities == "Seattle"]
print(hometown)
```

5. Create a vector that is the numbers four through 40, counting up by four, and save it
in a variable.
a.Create a loop that returns your favorite movie quote if the element is greater than 10
or the title of your favorite book if it isn’t.
b.Create a new loop that returns your favorite movie quote if the element is greater than
10 but less than 20, the title and artist of the last song you listened to if it’s greater
than or equal to 20 but less than 30, and the title of your favorite book if it’s greater
than or equal to 30.
```{r}
number_vector <- seq(4, 40, by = 4)
print(number_vector)

favorite_movie_quote <- "You're the weak one. And you’ll never know love, or friendship.
And I feel sorry for you."
favorite_book_title <- "Harry Potter"

for (num in number_vector) {


if (num > 10) {
print(favorite_movie_quote)
} else {
print(favorite_book_title)
}
}

favorite_movie_quote <- "You're the weak one. And you’ll never know love, or friendship.
And I feel sorry for you."
last_song_title <- "Hen Gap Em Duoi Anh Trang"
favorite_book_title <- "Harry Potter"

for (num in number_vector) {


if (num > 10 & num < 20) {
print(favorite_movie_quote)
} else if (num >= 20 & num < 30) {
print(last_song_title)
} else if (num >= 30) {
print(favorite_book_title)
}
}
```

6. Create a loop that prints “Coding is fun” five times.


a. Describe one alternative way you could achieve the same result.
```{r}
for (i in 1:5) {
print("Coding is fun")
}
print(rep("Coding is fun", 5))
```

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy