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