Course Outline

list High School / Statistics and Data Science I (AB)

Book High School / Statistics and Data Science I (AB)
  • 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.

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.

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).

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

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.

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.

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