2.18 Problem Set
2.18 Problem Set
print(class(numeric_variable))
print(class(character_variable))
print(class(logical_variable))
print(class(numeric_variable))
print(class(character_variable))
print(class(logical_variable))
```
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.
```
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"
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"