13) Selecting from a Matrix.

This Blog entry is from the Data Structures section in Learn R.

As a matrix is made up of vectors, it is logical to expect it to bear some resemblance in the way selection from a matrix takes place.   All subscripting in a separate dimension when specified inside the [] square brackets, as separate arguments.  The first argument inside the square brackets relates to the row, the next the column.

To obtain the value in a given position of a matrix, in this case two down, three across, type:

OverspillMatrix[2,3]
selecting-a-variable-from-a-matrix-in-r.png

Run the line of script to console:

selecting-a-variable-from-a-matrix-written-to-r-console.png

It can be seen that the value 2 has been returned which corresponds to the position specified:

a-validation-of-the-value-returned-from-the-matrix-in-r-console.png

7) Selecting and Filtering from a Character Vector

This Blog entry is from the Data Structures section in Learn R.

Once a Vector has been named,  attaching a label to each value,  it can be selected using the [] square bracket structure.  In this example,  the age for Tom needs to be extracted by typing:

Ages["Tom"]
a-script-to-select-a-value-from-a-vector-by-name.png

Run the line of script to console:

a-selected-variable-from-a-vector-by-name-written-to-r-console.png

Tom’s age is returned as 22, rather the value in the Vector carrying the label "Tom" is returned as 22. 

To select more than one label, it is a matter of creating a Vector with the criteria then passing that Vector inside the [] square brackets.  In this example, selecting Tom and Dick:

Ages[c("Tom","Dick")]
selecting-more-than-one-name-from-vector-in-r-script.png

Run the line of script to console:

more-than-one-named-variable-returned-to-r-console.png

5) Selecting and Filtering from a numeric Vector.

This Blog entry is from the Data Structures section in Learn R.

There are a number of ways to specifically extract data from a vector, a process sometimes called subscripting.  In this Blog entry, the vector created beforehand will be used.  The simplest way to extract data from a vector is to specify the position inside square brackets.  To subscript and retrieve the third value in the vector type:

SequenceBasic[3]

selecting-a-variable-from-a-vector-in-r.png

Run the line of script to console:

variable-selected-from-a-vector-written-to-r.png

It can be observed that the value at the third position in the SequenceBasic vector has been returned.  Alternatively, specifying a negative value of 3 would return everything except the third position:

excluding-a-variable-from-the-return-of-a-vector-in-r.png

Run the line of script to console:

a-vector-returned-to-the-r-console-excluding-value.png

It can be observed that the third position of the vector has been excluded in the output.

Far more powerful is the ability to select from vectors based upon a logical statement, such as all values > 5:

SequenceBasic[SequenceBasic > 5]
returning-only-variables-that-are-greater-in-vector-in-r.png

Run the line of script to console:

only-variables-that-are-greater-than-a-value-returned-in-r-console.png

It can be seen that only values greater than five have been returned.  The notion of selecting from a vector based on logical conditions further introduces operators:

·         & And.

·         | Or.

·         ! Not.

To create more discriminating selection from a vector, where the value must be > 2 and less than 5, type:

SequenceBasic[SequenceBasic > 2 & SequenceBasic <5]
vector-filtering-using-and-logic-script-in-r.png

Run the line of script to the console:

logical-filtering-returning-variables-from-vector-written-to-r-console.png

It can be seen that only the two values between 2 and 5 have been returned.