Select Page

## Archives

Posted on November 14, 2018
Author: Linda Stewart, Performance Architects

I hope you are enjoying the “Learning R Programming for Free” series; here are links to the previous segments (Step One, Step Two, Step Three, Step Four, Step Five) to provide some helpful background.

In the previous installment, we learned how to pull our own CSV data files into R Studio and we explored the basic mathematical operators we would want to use to do calculations on values in our data sets.

In this segment, I discuss R data types and work with the vector data structure to enter data directly into R.  R offers character, numeric and logical data types.

Let’s start by looking at a vector:

> vegas <- “What happens in Vegas, stays in Vegas!”

Then we can use “class()” to find out the data type of the vector:

> class(vegas)
 “character”

If we want to find out more about the “class” function, we can access the “help” file:

> help(class)

Or

> ?(class)

More complex vectors can be created using the “c()” or “combine” function to create lists.  Notice how the next three lists are created and automatically cast to the proper data type:

>numeric_vector <- c(1, 10, 49)
> class(numeric_vector)
 “numeric”
>
> character_vector <- c(“a”, “b”, “c”)
> class(character_vector)
 “character”
>
> # Complete the code for boolean_vector
> boolean_vector <- c(TRUE, FALSE,TRUE)
> class(boolean_vector)
 “logical”

What if we try to create a mix of character and numbers in a vector?

>mixed_vector <- c(“a”, “b”, 1)
> class(mixed_vector)
 “character”

It turns out that our “1” value is forced to be a character even though we did not put it in quotes.

It is helpful to be able to label our values in a vector.  We can do this with the “names()” function.

>my_profession_vector <- c(“Peyton Manning”, “Quarterback”)
> names(my_profession_vector) <- c(“Name”, “Profession”)
> my_profession_vector
Name       Profession
“Peyton Manning”    “Quarterback”

Numeric Vectors allow us to do math on the vectors:

>vector_sum1 <- c(1, 2, 3) + c(4, 5, 6)
> vector_sum1
 5 7 9
> vector_sum2 <- c(1 + 2, 3 + 4, 5 + 6)
> vector_sum2
  3  7 11
>
> vector1 <- c(1, 2, 3)
> vector2 <- c(4, 5, 6)
>

Take the sum of “vector1” and “vector2”:

>total_vector <- vector1 + vector2
>
> # Print out total_vector
> total_vector
 5 7 9

We can use the “sum()” function to add up the values in a numeric vector:

>vector_sum1 <- c(1, 2, 3) + c(4, 5, 6)
> total <- sum(vector_sum1)
> total
 21

We can also compare our numeric vectors to see if one is larger than another.  Let’s look at whether we are
better at blackjack than poker:

>blackjack_winnings <- c(20, 50, -100, 60, -20)
> poker_winnings <- c(120, -60, 150, -90, 40)
> total_blackjack <- sum(blackjack_winnings)
> total_poker <- sum(poker_winnings)

Are our blackjack winnings less than our poker winnings?

>total_blackjack < total_poker
 TRUE

We can also reference specific elements of our vector by using square brackets [ ].  Let’s print element 3 from our poker_winnings vector:

>poker_winnings <- c(120, -60, 150, -90, 40)
> poker_winnings
 150

We can also print several values like this:

>poker_winnings <- c(120, -60, 150, -90, 40)
> poker_winnings[c(2,3)]
 -60 150
> poker_winnings[2:4]
 -60 150 -90

What if our values were associated with different tables at the casino?  Let’s name the tables:

>poker_winnings <- c(120, -60, 150, -90, 40)
> names(poker_winnings) <- c(“T1”, “T2”, “T3”, “T4”, “T5”)

Let’s look at the winnings from tables 2, 3, and 4:

> poker_tables <- poker_winnings[c(“T2″,”T3″,”T4”)]

Now let’s find the average winnings from these tables using the “mean()” function:

>mean(poker_tables)
 0

Not too great, but at least we didn’t lose any money!

As we saw earlier, we can apply comparison operators to our vectors.  The list of comparison operators includes:

<
>
<=
>=
==
!= In our next installment, we will continue our discussion of vectors with more examples of how to compare vectors, as well as how to work with NULL and missing data in our data structures in R.