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.