R Basics II

Classwork 2

Author

Byeong-Hak Choe

Published

September 18, 2024

Modified

October 5, 2024

Question 1.

  • Write an R code to create a numeric vector named numbers containing the integers from 10 to 50.

Answer:

numbers <- 10:50
numbers
 [1] 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
[26] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  • The colon operator : in R is used to generate a sequence of integers. In this case, 10:50 generates the integers from 10 to 50 and assigns them to the variable name numbers.



Question 2.

  • Create a logical vector logical_vec that checks whether the elements of the vector ages <- c(15, 22, 18, 24, 30) are greater than or equal to 20.
ages <- c(15, 22, 18, 24, 30)

Answer:

logical_vec <- ages >= 20
logical_vec
[1] FALSE  TRUE FALSE  TRUE  TRUE
  • The comparison operator >= checks each element of the ages vector to see if it’s greater than or equal to 20. The result is a logical vector of TRUE or FALSE values for each corresponding element.



Question 3.

  • Write an R code to assign the string “Hello, World!” to a variable named greeting and display its value on the Console.

Answer:

greeting <- "Hello, World!"
greeting
[1] "Hello, World!"
  • The character value “Hello, World!” is assigned to the variable greeting, and the value is displayed by simply typing the variable name, which prints its content to the Console.



Question 4.

  • Write an R code to calculate the range (difference between the maximum and minimum values) of the vector temp <- c(22, 28, 31, 25, 29).
temp <- c(22, 28, 31, 25, 29)

Answer:

range_value <- max(temp) - min(temp)
range_value
[1] 9
  • The range is calculated by subtracting the minimum value of the vector from the maximum value. The max() function gives the maximum value, and the min() function gives the minimum value.



Question 5.

  • Write an R code to convert the character vector char_vec <- c("1", "2", "3", "4") into a numeric vector named num_vec.
char_vec <- c("1", "2", "3", "4")

Answer:

num_vec <- as.numeric(char_vec)
num_vec
[1] 1 2 3 4
  • The as.numeric() function is used to convert the elements of char_vec, which are characters, into numeric values. The resulting numeric vector is stored in num_vec.



Question 6.

  • Write an R code to concatenate two character vectors, first_names <- c("John", "Jane") and last_names <- c("Doe", "Smith"), to create a vector full_names containing the full names using the str_c() function for vectorized string operations.
first_names <- c("John", "Jane")
last_names <- c("Doe", "Smith")

Answer:

library(stringr)
full_names <- str_c(first_names, last_names, sep = " ")
full_names
[1] "John Doe"   "Jane Smith"
  • The str_c() function from the one of the packages in tidyverse is used to concatenate two vectors (first_names and last_names) element-wise. The sep = " " argument ensures that a space is inserted between the first and last names.



Discussion

Welcome to our Classwork 2 Discussion Board! 👋

This space is designed for you to engage with your classmates about the material covered in Classwork 2.

Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.

If you have any specific questions for Byeong-Hak (@bcdanl) or peer classmate (@GitHub-Username) regarding the Classwork 2 materials or need clarification on any points, don’t hesitate to ask here.

Let’s collaborate and learn from each other!

Back to top