19) Create a Data Frame from Names and stringsAsFactors.

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

As introduced previously the data.frame() function, not unlike the list() function, has more flexibility to be able to create objects than the c() function.   As seems intuitive it is possible to specify names explicitly rather than take the names of the Vectors by default.  There is an argument to the data.frame() function that can ease the burden of creating factors upon detection of character vectors in the for of the stringsAsFactors switch (although it is not always sensible to use it in the case of numeric prediction focus). 

To create a Data Frame with specific names and disabling stringsAsFactors:

LabeledDataFrame <- data.frame(data.frame(ExampleFullNames = FullNames,ExampleFullAges = FullAges,ExampleFullGender = FullGender,stringsAsFactors = FALSE))
a-script-in-r-to-add-a-names-to-a-data-frame.png

Run the line of script to console:

new-data-frame-with-names-written-to-r-console.png

Return the Data Frame by typing:

a-script-to-write-out-a-labeled-data-frame-in-r.png

Run the line of script to console:

a-named-data-frame-written-out-to-the-r-console.png

It can be observed that the column names have been correctly specified.  Unless a factor has been specifically allocated it can be trusted that other character Vectors,  such as FullName in this example, will not be transposed to factors automatically.

12) Labeling a Matrix.

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

It is helpful for reference to label a Vector.  It is possible also to label the rows and the columns of a matrix in a similar fashion using the rownames() and colnames() function.

To set column names assign a Vector to the colnames() function, where the colnames() function accepts the matrix as its argument:

colnames(OverspillMatrix) <- c("Example1","Example2","Example3","Example4")
a-vector-of-column-names-in-r-script.png

Run the line of script to console:

a-vector-of-column-names-created-and-written-out-to-r-console.png

The rownames() function has a similar signature and takes a Vector of row names:

rownames(OverspillMatrix) <- c("Row1","Row2","Row3")
script-to-create-row-names-in-r.png

Run the line of script to console:

script-to-create-row-names-in-r-written-out-to-r-console.png

The matrix is now labelled in both directions and can be inspected by typing:

OverspillMatrix
script-to-confirm-that-a-matrix-is-now-names-on-columns-and-rows.png

Run the line of script to console:

a-fully-named-matrix-written-out-to-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

6) Setting Vector Labels \ Names

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

Selecting from a chracter vector follows the same pattern,  in so far as the crieria sits inside [] square brackets and allows for the specifc selection of values or the specific exclusion of values.  Create a chracter vector by typing numbers,  hencforth ages:

Ages <- c(22,23,28)
creating-a-simple-numeric-vector-for-ages-in-r.png

Run the line of script to console:

creating-a-basic-ages-vector-written-to-r-console.png

It is possible to add labels to the entries in the vector using the names() function, similar to column headers in an Excel spreadsheet.   The label 22 is Tom’s Age, 23 is Harries Age and lastly 28 is Dicks Age.  To add labels to each Vector value, type:

names(Ages) <- c("Tom","Harry","Dick")
creating-a-string-vector-to-match-to-ages-vector-in-r.png

Run the line of script to console:

creating-a-vector-of-names-to-match-to-ages-vector-written-to-r-console.png

It can be observed that the Vector in the environment pane is now marked as being a ‘Named’ vector:

a-named-vector-showing-in-environment-of-rstudio.png

Outputting the Vector to console, type:

a-script-to-write-out-ages-vector-in-r.png

Run the line of script to console:

named-vector-written-out-to-r-console.png

It can be observed that the vector more closely resembles the row of a spreadsheet.  The names function will be used more extensively when aggregating Vectors into a Matrix, for the time being however, it will be used to allow for the selection of just that individuals Age.