Course Outline

list High School / Statistics and Data Science II (XCD)

Book
  • College / Advanced Statistics and Data Science (ABCD)
  • College / Statistics and Data Science (ABC)
  • 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)

1.4 Save Your Work in R Objects

Have you ever had an experience where you have forgotten to save your work? It’s a terrible feeling. Saving your work is also important in R. In R, we don’t just do calculations and look at the results in the output window. We usually save the results of the calculations somewhere so we can find them later.

Pretty much anything, including the results of any R function, can be saved in an R object. This is accomplished by using an assignment operator, which looks kind of like an arrow (<-).

Here’s a simple example to show how it’s done. Let’s make up a name for an R object; we will call it my_favorite_number. Then let’s think of what our favorite number is (say, 20), and save it in the R object. Go ahead and run the code below to see how this works.

# This code will assign the number 20 to the R object my_favorite_number my_favorite_number <- 20 # This code prints out my_favorite_number. Notice that you don't need to use the print() function to print the contents of an R object; you can just type the name of the object my_favorite_number # now revise the code to use your actual favorite number # This is an example --- use your favorite number! my_favorite_number <- 17 my_favorite_number ex() %>% { check_object(., "my_favorite_number") check_output_expr(., "my_favorite_number") }
CK Code: X1_Code_Save_01

Now remember, R is case sensitive. Try assigning 5 to num and 10 to NUM.

# Assign 5 to num and 10 to NUM num <- NUM <- # Write the name of the object that contains 10 # This will print out the contents of that object num <- 5 NUM <- 10 NUM msg_undefined <- "Make sure to define both variables: num and NUM." msg_incorrect <- "Make sure you assign the correct value to each variable." msg_not_print <- "Don't forget to print out the object that contains 10." ex() %>% { check_object(., 'num', msg_undefined) %>% check_equal(msg_incorrect) check_object(., 'NUM', msg_undefined) %>% check_equal(msg_incorrect) check_output_expr(., "NUM", missing_msg = msg_not_print) }
CK Code: X1_Code_Save_02

Note: When you save an R object in one of the code windows it will only be saved until you leave the page. If you re-load the page later it won’t be there.

Vectors

We’ve used R objects so far to store a single number. But in statistics we are dealing with variation, which by definition means more than one—and sometimes many—numbers. An R object can also store a whole set of numbers, called a vector. You can think of a vector as a list of numbers (or values).

The R function c() can be used to combine a list of individual values into a vector. You could think of the “c” as standing for “combine.” So in the following code we have created two vectors (we just named them my_vector and my_vector_2) and put a list of values into each vector.

# Here is the code to create two vectors my_vector and my_vector_2. We just made up those names. # Run the code and see what happens my_vector <- c(1, 2, 3, 4, 5) my_vector_2 <- c(10, 10, 10, 10, 10) # Now write some code to print out these two vectors.# Here is the code to create two vectors my_vector and my_vector_2. We just made up those names. # Run the code and see what happens my_vector <- c(1, 2, 3, 4, 5) my_vector_2 <- c(10, 10, 10, 10, 10) # Now write some code to print out these two vectors. my_vector # or print(my_vector) my_vector_2 # or print(my_vector_2) ex() %>% { check_object(., 'my_vector') check_object(., 'my_vector_2') check_output_expr(., "my_vector") check_output_expr(., "my_vector_2") }
CK Code: X1_Code_Save_03

If you ask R to perform an operation on a vector, it will assume that you want to work with the whole vector, not just one of the numbers.

So if you want to multiply each number in my_vector by 100, then you can just write my_vector * 100. Try it in the code window below.

my_vector <- c(1, 2, 3, 4, 5) # write code to multiply each number in my_vector by 100 my_vector <- c(1, 2, 3, 4, 5) # write code to multiply each number in my_vector by 100 my_vector * 100 ex() %>% { check_object(., "my_vector") %>% check_equal() check_operator(., "*") %>% check_result() %>% check_equal() }
CK Code: X1_Code_Save_04

Notice that when you do a calculation with a vector, you’ll get a vector of numbers as the answer, not just a single number. This is very useful for working with data!

Remember, R will do the calculations, but if you want something saved, you have to assign it somewhere. Try writing some code to compute my_vector * 100 and then assign the result into a new object called my_vector_100.

require(coursekata) # This creates my_vector and stores 1, 2, 3, 4, 5 in it my_vector <- c(1, 2, 3, 4, 5) # Now write code to save my_vector * 100 back into my_vector my_vector_100 <- # This creates my_vector and stores 1, 2, 3, 4, 5 in it my_vector <- c(1, 2, 3, 4, 5) # Now write code to save my_vector * 100 back into my_vector my_vector_100 <- my_vector * 100 ex() %>% { check_operator(., "*") %>% check_result() %>% check_equal() check_object(., "my_vector") %>% check_equal() }
CK Code: X1_Code_Save_05

You might be a bit confused because nothing printed out. Well, did you tell R to print something? Or, did you tell it to save something? If you want to see something printed out, feel free to go back to the code window and print out my_vector_100.

There may be times when you just want to know one of the values in a vector, not all of the values. We can index a position in the vector by using brackets with a number in it like this: [1]. So if we wanted to print out the contents of the first position in my_vector, we could write my_vector[1].

require(coursekata) my_vector <- c(1,2,3,4,5) my_vector_100 <- my_vector * 100 # Write code to get the 4th value in my_vector my_vector[4] ex() %>% check_output_expr( "my_vector[4]", missing_msg = "Have you used `[4]` to print out the 4th number in `my_vector`?" )
CK Code: X1_Code_Save_06

Many functions will take in a vector as the input. For example, try using sum() to total up the five values saved in my_vector. Note that we have already saved some values in my_vector for you.

require(coursekata) my_vector <- c(2, 4, 6, 8) # Use sum() to total up the values in my_vector sum(my_vector) ex() %>% { check_object(., "my_vector") check_function(., "sum", not_called_msg = "don't forget to use the sum() function") %>% check_result() %>% check_equal(incorrect_msg = "did you call sum() on my_vector?") }
CK Code: X1_Code_Save_07

R for Humans

Programming languages are primarily for communicating with computers. But there are a lot of things we do when we write R just to make it easier for humans to understand. 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.

Responses