Saturday, July 15, 2017

(R) Vector Creation and Extraction

In this article, we will discuss how to create vectors manually, and also, how to create vectors from data contained within existing vectors.

Just as a reminder, a vector is a sequence of data elements of the same basic type. Each element in a sequence is referred to as a component.*

Elements of a vector are displayed to the console such as:

[1] 3 5 7 9


However, just because the data is printed as a row, does not mean that the data cannot be added to an existing data frame as a column. Therefore, though vector data prints to the console horizontally, you can imagine it as also existing vertically, like so:

3
5
7
9


The [1] indicates the beginning of the vector. If the console runs out of horizontal space while printing the vector, the remainder of the vector will be printed to the next line of the console, the new line will begin with a value which indicates the sequence order.

For Example:

[1] 1 2 5 7 9
[6] 11 13 15 17


Vector Creation

Here are a few examples vector creating code:

x <- seq(from=2, to=12, by=2)

This creates a vector which contains the values: 2 4 6 8 10 12

The code is instructing R to count from 2 to 12, by 2, and then store the values in vector 'x'.

x <- rep(seq(from=2, to=12, by=2), times=2)

This creates a vector which contains the values: 2 4 6 8 10 12 2 4 6 8 10 12

The code is instructing R to count from 2 to 12, by 2, twice, and then store the values in vector 'x'.

x <- rep(c("o", "m", "g"), times=3)

This creates a vector which contains the values: O M G O M G O M G

This code is instructing R to repeat the values "O", "M" "G", three times, and then store the values in vector 'x'.

Now, let's say that you want to manipulate the data within the vectors, here are few methods.


Vector Data Manipulation

Adding Within Vectors

x <- x + 10 


This code adds the value of 10 to each value within vector x, and then stores the values within vector 'x'.

So if vector 'x' contained the data: 1 2 3 4 5 6

The above code would modify the vector so that it contained the data: 11 12 13 14 15 16


Multiplying Within Vectors

x <- x * 0

This code multiplies each value within vector 'x' by the value of 0. The values are then subsequently stored within vector 'x'.

So if vector 'x' contained the data: 1 2 3 4 5 6

The above code would modify the vector so that it would contain the data: 0 0 0 0 0 0

If two vectors are of the same length, they may be added, subtracted, multiplied or divided.

For Example:

If vector 'y' contained the values: 2 2 2 2 2

And vector 'x' contained the values: 2 2 2 2 2

The Code:

w <- x + y

Would generate 'w' as a vector, containing the values of: 4 4 4 4 4

If vectors of different lengths are combined in this way...

For Example:


If vector 'y' contained the values: 2 2 2 2 2

And vector 'x' contained the values: 2 2 2 2 2

w <- x + y

Would present the user with the error:

Warning message: In x + y : longer object length is not a multiple of shorter object length.

Extracting From Vectors

Assuming that 'u' is a vector which contains the values of: 1 2 3 4 5 6

q <- u[3]

This example would extract the third value of the vector 'u', and store the data in the vector 'q'.

Therefore, 'q' would contain a value of 3.

q <- u[-3]

In this case, all values but the third value of vector 'u' would be extracted, and the data would be stored in vector 'q'.

Therefore, 'q' would contain the values: 1 2 4 5 6

q <- u[1:2]

Here, the first two values are extracted from u, and stored in vector 'q'.

Therefore, 'q' would contain the values: 1 2

q <- u[c(1,2)]

This is another way of extracting the first two values of 'u', which will then be stored in vector 'q'.

Again, 'q' would contain the values: 1 2

q <- u[-c(1,2)]

In this example, all values from vector 'u' are extracted, except the values of 1 and 2.

Therefore, vector 'q' would contain the values: 3 4 5 6

q <- u[u<4]

This method extracts all values within vector 'u' that are less than 4.

It is for this reason, the vector 'q' would contain the values: 1 2 3

* - www.r-tutorial.com/r-introduction/vector

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.