Classwork 4

R Basics I

Author

Byeong-Hak Choe

Published

September 22, 2025

Modified

September 25, 2025

Question 1.

  • In R, the object state.name is available without loading any additional packages.
  • Write R code to assign state.name to a new variable called US_states.

Example: calling state.name in R

Answer:



Question 2.

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

Answer:



Question 3.

The temp_F vector contains the average high temperatures in January for the following cities: Seoul, Lagos, Paris, Rio de Janeiro, San Juan, and Rochester.

temp_F <- c(35, 88, 42, 84, 81, 30)

Create a new vector named temp_C that stores the converted Celsius temperatures. Below is the conversion formula:

\[ C = \frac{5}{9}\times(F - 32) \]

Answer:



Question 4.

Write an R code to assign the string โ€œHello, World!โ€ to a variable named greeting and display its value on the Console.

Answer:



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:



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 (e.g., โ€œJohn Doeโ€, โ€œJane Smithโ€) using the str_c() function for vectorized character operations.
    • Note that the str_c() function is provided by the stringr package, which is one of the packages in the tidyverse.
first_names <- c("John", "Jane")
last_names <- c("Doe", "Smith")

Answer:



Back to top