This tutorial is going to cover how to create a data frame in R. We’re going to get through how to:

  • Create data frames from Vectors.
  • Create data frames from existing data frames.
  • Create data frame a List.

1. Create A Data Frame In R From Vectors

This section is going to describe how to create data frames from R Vectors, a very popular and “natural” way.

1.1. Quick Syntax

Let’s see a quick syntax to create a data frame in R from a list of vectors using the data.frame function

Where v1, v2, v3,… is the list of Vectors in R. For example, let’s assume we have following vectors represented for variables of books:

We can create a data frame by using the following command:

As mentioned in the definition of the Data Frame in R, it is just a “list of variables of the same number of rows”, we simply provide the list of vectors/variables for the data.frame function. Now, let’s print the data frame into console to see how it looks like:

We can see that the data frame has 3 columns, each of which corresponds to an above vector/variable.

1.2. Full Syntax

Below is the full syntax to create a data frame in R:

Here are the details of the syntax:

ArgumentsDescriptions
values, or functions which can be evaluated as a list of vectors
row.namesA single integer or character string specifying a column to be used as row names, or a character or integer vector giving the row names for the data frame
check.namesLogical, if TRUE then the rows are checked for consistency of length and names.
fix.empty.namesLogical, indicating if arguments which are “unnamed” (in the sense of not being formally called as someName = arg) get an automatically constructed name or rather name “”. Needs to be set to FALSE even when check.names is false if “” names should be kept.
stringsAsFactorsLogical: should character vectors be converted to factors?

2. Creating Data Frames From Existing Data Frames

In some cases, we just want to create a new data frames from existing ones, where we take just some interested columns, or subset some columns based on some conditions.

Let’s say we have a data frame df as follows:

And now we want to create a new data frame from it, or we simply want to copy it into a new data frame. Let’s see how we can do that by passing it to the data.frame function:

Let’s see the the new data frame dfNew:

In some situation, we just want only several columns of the existing data frame in our new data frame, we can do that by selecting a subset of our interested columns, as follows:

On the above example, we have just created a new data frame dfNew from 2 columns x and y of the data frame df. Now, let’s see its content:

3. Creating Data Frames From A Matrix

To create data frames from a Matrix in R, we can use the function as.data.frame which has the syntax as follows:

Let’s create a matrix with 2 rows and 3 columns as below:

And print out its content:

Now let’s create a new data frame from the above matrix:

And print out the content of the created data frame:

4. Create An Empty Data Frame In R

Sometimes, we just want to create an empty data frame. This section is going to cover several approach to do such.

4.1. By Initializing A List Empty Vectors

The easiest way to create an empty data frame is to provide a list of empty vectors for the data.frame function. Let’s see a below example where we will create an empty data frame with 3 columns:

Let’s print the structure of the above data frame:

It has 3 columns and 0 rows.

4.2. By Initializing A Matrix with Zero Rows

Another way to create an empty data frame is to providing the data.frame function a matrix with zero rows. Let’ see an example below:

At the first command, we create a data frame by providing the data.frame with a matrix with 3 columns and 0 rows. From the 2nd row, we change the column names of the data frame.

Refer to R – Rename Column of Data Frame to change or rename columns of data frame in R.

Or we can do in one-shot as follows:

Let’s see the structure of the data frame:

5. Conclusion

We have just gotten through how to create a data frame from a list of vectors/variables. And finally, data frame is a very fundamental data structure in R, how to manipulate it can be found at following links:

Add New Column To A Data Frame in R

Remove Or Delete A Column Of A Data Frame In R

How To Order A Data Frame In R

0 0 vote
Article Rating