Working with Vectors: Step Seven in Learning R Programming for Free

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, Step Six) to provide some helpful background.

In the previous installment, we learned to work with vectors and how to complete some basic operations on sets of vectors.

In our last discussion, we ended with this table of comparison operators available for us to use to test the values in the vectors.  In this discussion, let’s apply these tests.

To add comments to R, we prefix the line with the hash “#” symbol:

# poker and blackjack winnings from Monday to Friday:

>blackjack_winnings <- c(120, 60, -80, 40, -25) > poker_winnings <- c(40, -50, 250, -25, 140) > dow_vector <- c(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”) > names(blackjack_winnings) <- dow_vector > names(poker_winnings) <- dow_vector > total_blackjack <- sum(blackjack_winnings) > total_poker <- sum(poker_winnings) > > # Let’s find out at the new casino, on what days did we make money on poker?

We can find out on which days of the week we made money by logically testing each member of the vector:

> winning_dow_vector <- poker_winnings > 0

We can then print the days that we won money, and how much was won in poker:

>poker_winnings Monday   Tuesday Wednesday  Thursday    Friday 40       -50       250       -25       140 > winning_dow_vector <- poker_winnings > 0 > winning_dow_vector >Monday   Tuesday Wednesday  Thursday    Friday TRUE     FALSE      TRUE     FALSE      TRUE

poker_winnings[winning_dow_vector] Monday Wednesday    Friday 40       250       140

So, it already looks like our new casino has potential.

Let’s take a look at a few more functions that work with vectors:

typeof(x) length(x) str(x)

The “type of” function takes any object “x” and returns its type:

> typeof(poker_winnings[winning_dow_vector]) [1] “double”

The “length” function takes any object “x” and returns the length of vectors, including lists and factors:

> length(poker_winnings[winning_dow_vector]) [1] 3

The “str” function takes any object “x” and displays an abbreviated informational output summarizing the contents of the object:

> str(poker_winnings[winning_dow_vector]) Named num [1:3] 40 250 140 – attr(*, “names”)= chr [1:3] “Monday” “Wednesday” “Friday”

Something you will eventually see in data dets are “NULL” or “NA” values. “NaN” means “Not a Number” and means the value is undefined.  When we use functions like “mean()” or “sum()” on a vector containing “NA” or “NaN,” if there are empty values, functions like mean and sum will return “NA” and “NaN”.  We can logically test for “NA” and “NaN” and use “is.null” and “” to trim those values from the data set:

> x <- c(1, 2, NA, 4, 5, 6, NA, 8, NULL)   # Create a numeric vector > x                                        # There’s an NA in there [1]  1  2 NA  4  5  6 NA  8 > > x * 2                                    # If any NA, returns NA [1]  2  4 NA  8 10 12 NA 16 > > sum(x) [1] NA > mean(x) [1] NA

One last thing to note: We have to watch our white spaces in R.  Let’s look at this:


Question: “Are you assigning 3 to x or are you trying to ask if x is less than -3?”

x <- 3


x < -3

In our next installment, we will continue our discussion of R data structures by looking at the data frame.

Additional blog posts on more complex R concepts to follow; please contact if you have any questions or need further help!