Course Outline

list High School / Advanced Statistics and Data Science I (ABC)

Book
  • High School / Advanced Statistics and Data Science I (ABC)
  • High School / Statistics and Data Science I (AB)
  • High School / Statistics and Data Science II (XCD)
  • High School / Algebra + Data Science (G)
  • College / Introductory Statistics with R (ABC)
  • College / Advanced Statistics with R (ABCD)
  • College / Accelerated Statistics with R (XCD)
  • CKHub: Jupyter made easy

1.6 What You Can Store in R Objects

You can think of R objects like buckets that hold values. An R object can hold a single value, or it can hold a group of values (as in the case of a vector). So far, we have only put numbers into R objects. But R objects can actually hold three types of values: numbers, characters, and Boolean values.

Numerical Values

If R knows that you are using numbers, it can do lots of things with them. We have seen, for example, that R can perform arithmetic operations on numbers: addition, subtraction, multiplication, and division.

# Here are two ways of creating a numeric vector with the numbers 1 to 10 my_num_1 <- c(1,2,3,4,5,6,7,8,9,10) my_num_2 <- 1:10 # Write code to print out both of these numeric vectors # Here are two ways of creating a numeric vector with the numbers 1 to 10 my_num_1 <- c(1,2,3,4,5,6,7,8,9,10) my_num_2 <- 1:10 # Write code to print out both of these numeric vectors my_num_1 my_num_2 ex() %>% { check_object(., "my_num_1") %>% check_equal() check_object(., "my_num_2") %>% check_equal() check_output_expr(., "my_num_1", times = 2, missing_msg = "Did you print out both my_num_1 and my_num_2?", append = FALSE) }

Note that in R when we use a colon like this, 1:10, it means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. That’s pretty convenient. Imagine if you needed a vector with the numbers from 1 to 10,000! The colon would be a big time saver.

Character Values

Characters are comprised of text, such as words or sentences. (Numbers can also be treated as characters, depending on the context. For example, when 20 is in quotation marks like this – “20” – it will be treated as a character value, even though it includes a number.) Character values are in between quotation marks, " “. (R doesn’t usually care whether you use single quotes, ‘like this’, or double quotes,”like this".) We’ll mostly use double quotes for consistency.

If we forget the quotes, R will think that a word is a name of an object instead of a character value.

many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba") # Write code to print out the 5th way of saying hello in this vector many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba") # Write code to print out the 5th way of saying hello in this vector many_hellos[5] ex() %>% { check_object(., "many_hellos") %>% check_equal(incorrect_msg = "Make sure not to change the contents of many_hellos") check_output_expr(., "many_hellos[5]", missing_msg = "You can use [] to select the 5th element in many_hellos") }

Boolean Values

Boolean values are either TRUE or FALSE. Maybe we have a question such as: Is the first element in the vector many_hellos “hi”? We can ask R to find out and return the answer TRUE or FALSE. We can do that by using the comparison operator == (it just means equal).

require(coursekata) many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba") # See what happens when you submit this code: many_hellos[1]== "hi" # See what happens when you submit this code: many_hellos[1]== "hi" ex() %>% check_output_expr("many_hellos[1]=='hi'", missing_msg = "Make sure you don't change the code before pressing Submit")

If we want, we can store that answer in an R object.

require(coursekata) many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba") # Write some code that will answer this question: Is the first element in the vector many_hellos "hi"? # And store it in an R object called first_is_hi # Write some code that will answer this question: Is the first element in the vector many_hellos "hi"? # And store it in an R object called first_is_hi first_is_hi <- many_hellos[1] == "hi" ex() %>% { check_operator(., "==") %>% check_result() %>% check_equal() check_object(., "first_is_hi") %>% check_equal() }

Most of the questions we ask R to answer with a TRUE or FALSE involve comparison operators such as >, <, >=, <=, and ==. The double == sign checks if two values are equal. There is even a comparison operator to check whether values are not equal: !=. For example, 5 != 3 is a TRUE statement.

# Read this code and predict what value will come out of the R console. Then run the code and see if you were right. A <- 1 B <- 5 compare <- A > B compare # Read this code and predict what value will come out of the R console. Then run the code and see if you were right. A <- 1 B <- 5 compare <- A > B compare ex() %>% { check_object(., "A") %>% check_equal(incorrect_msg = "Make sure not to change the contents of A") check_object(., "B") %>% check_equal(incorrect_msg = "Make sure not to change the contents of B") check_object(., "compare") %>% check_equal(incorrect_msg = "Make sure not to change the contents of compare") check_output_expr(., "compare", missing_msg = "Make sure to print compare") }

Note that compare in the code above is not a function. You know this because there is no () after it. compare, in this case, is just a name we made up for an R object to store the Boolean result of the question, “Is A greater than B?”. The answer, as we can see, is FALSE.

We can also create Boolean vectors by subjecting a whole vector to a comparison. Let’s create a numeric vector with the numbers from 1 to 10 (we will call this vector my_numbers). Then let’s create a Boolean vector called my_booleans to store the results of checking whether each number in the my_numbers vector is greater than or equal to 5.

# Here's the code to create the my_numbers vector: my_numbers <- 1:10 # And here's the code to check whether each element of the vector my_numbers is greater than or equal to 5, storing the result in a new vector called my_booleans. my_booleans <- my_numbers >= 5 # This code prints out both vectors my_numbers my_booleans # Here's the code to create the my_numbers vector: my_numbers <- 1:10 # And here's the code to check whether each element of the vector my_numbers is greater than or equal to 5, storing the result in a new vector called my_booleans. my_booleans <- my_numbers >= 5 # This code prints out both vectors my_numbers my_booleans ex() %>% { check_object(., "my_numbers") %>% check_equal(incorrect_msg = "Make sure to keep the line that assigns 1:10 to my_numbers") check_object(., "my_booleans") %>% check_equal(incorrect_msg = "Make sure you assign my_numbers >= 5 to my_booleans") check_output_expr(., "my_numbers", missing_msg = "Did you print my_numbers?") check_output_expr(., "my_booleans", missing_msg = "Did you print my_booleans?") }
# What do you expect from this code? Run the code to see what happens. Then, fix the bug and run again. A <- 5 B <- 5 compare <- A = B compare # What do you expect from this code? Run the code to see what happens. Then, fix the bug and run again. A <- 5 B <- 5 compare <- A == B compare ex() %>% { check_object(., "A") %>% check_equal(incorrect_msg = "Make sure the object A is assigned the value 5") check_object(., "B") %>% check_equal(incorrect_msg = "Make sure the object B is assigned the value 5") check_operator(., "==") %>% check_result() %>% check_equal() check_object(., "compare") %>% check_equal() check_output_expr(., "compare", missing_msg = "Did you tell R to print compare?") }

In R, we will avoid using the single equal sign, =. If you want to know whether A is equal to B, use the double equal sign, ==. The single equal sign is sometimes used instead of the assignment operator, <-, which can get confusing, both to you and to R. Use the arrow <- to assign values to an R object, and == to ask whether two values are equal.

R for Humans

Programming languages are primarily for communicating with computers. But there are a lot of things we do when we write R to communicate with humans. For example, R doesn’t care if we write spaces between things. We will write A <- 5 and we put spaces in there. But we don’t do it for R. R thinks that A<-5 is the same as A <- 5. We add the spaces to make it easier for a human to read. The same goes for comments (that begin with #); R will ignore that code but it may be useful for a human reading the code.

Also, we are mindful that R is a computer language and doesn’t actually “think” or “care” or “ignore” anything, but we will commonly anthropomorphize R. Many readers of this course are new to programming and it might be helpful to think about programming as communicating with R.

Responses