1.1
Type anything at the prompt, and R will evaluate it and print the answer.Let's try some simple math. Type the below command.- Type the string
"Arr, matey!"
. (Don't forget the quotes!)
CHAPTER 2
Vectors
The name may sound intimidating, but a vector is simply a list of values. R relies on vectors for many of its operations. This includes basic plots - we'll have you drawing graphs by the end of this chapter (and it's a lot easier than you might think)!Course tip: if you haven't already, try clicking on the expand icon () in the upper-left corner of the sidebar. The expanded sidebar offers a more in-depth look at chapter sections and progress.Vectors2.1
A vector's values can be numbers, strings, logical values, or any other type, as long as they're all the same type. Try creating a vector of numbers, like this:RedoComplete> c(4, 7, 9) [1] 4 7 9
Thec
function (c
is short for Combine) creates a new vector by combining a list of values.- Vectors cannot hold values with different modes (types). Try mixing modes and see what happens:RedoComplete
> c(1, TRUE, "three") [1] "1" "TRUE" "three"
All the values were converted to a single mode (characters) so that the vector can hold them all. Vector Math2.6
Most arithmetic operations work just as well on vectors as they do on single values. We'll make another sample vector for you to work with, and store it in thea
variable.If you add a scalar (a single value) to a vector, the scalar will be added to each value in the vector, returning a new vector with the results. Try adding 1 to each element in our vector:- You can also take two vectors and compare each item. See which values in the
a
vector are equal to those in a second vector:RedoComplete> a == c(1, 99, 3) [1] TRUE FALSE TRUE
Notice that R didn't test whether the whole vectors were equal; it checked each value in the a vector against the value at the same index in our new vector. Scatter Plots2.7
Theplot
function takes two vectors, one for X values and one for Y values, and draws a graph of them.Let's draw a graph showing the relationship of numbers and their sines.First, we'll need some sample data. We'll create a vector for you with some fractional values between 0 and 20, and store it in thex
variable.Now, try creating a second vector with the sines of those values:- Then simply call
plot
with your two vectors:RedoComplete> plot(x,y)
Great job! Notice on the graph that values from the first argument (x
) are used for the horizontal axis, and values from the second (y
) for the vertical. - Your turn. We'll create a vector with some negative and positive values for you, and store it in the
values
variable.We'll also create a second vector with the absolute values of the first, and store it in theabsolutes
variable.Try plotting the vectors, withvalues
on the horizontal axis, andabsolutes
on the vertical axis. NA Values2.8
Sometimes, when working with sample data, a given value isn't available. But it's not a good idea to just throw those values out. R has a value that explicitly indicates a sample was not available:NA
. Many functions that work with vectors treat this value specially.We'll create a vector for you with a missing sample, and store it in thea
variable.Try to get the sum of its values, and see what the result is:RedoComplete> a <- 3="" 7="" 9="" c="" na=""> sum(a) [1] NA ->
The sum is considered "not available" by default because one of the vector's values wasNA
. This is the responsible thing to do; R won't just blithely add up the numbers without warning you about the incomplete data. We can explicitly tellsum
(and many other functions) to removeNA
values before they do their calculations, however.- Remember that command to bring up help for a function? Bring up documentation for the
sum
function:RedoComplete> help(sum) sum package:base R Documentation Sum of Vector Elements Description: 'sum' returns the sum of all the values present in its arguments. Usage: sum(..., na.rm = FALSE) ...
As you see in the documentation,sum
can take an optional named argument,na.rm
. It's set toFALSE
by default, but if you set it toTRUE
, allNA
arguments will be removed from the vector before the calculation is performed. Chapter 2 Completed
Share your plunder:
You've traversed Chapter 2… and discovered another badge!In this chapter, we've shown you all the basics of manipulating vectors - creating and accessing them, doing math with them, and making sequences. We've shown you how to make bar plots and scatter plots with vectors. And we've shown you how R treats vectors where one or more values are not available.The vector is just the first of several data structures that R offers. See you in the next chapter, where we'll talk about… the matrix.More from O'Reilly
Did you know that our sponsor O'Reilly has some great resources for big data practitioners? Check out the Strata Newsletter, the Strata Blog, and get access to five e-books on big data topics from leading thinkers in the space.
No comments:
Post a Comment