R: How to Create a Data Frame from Vectors


Often you may want to create a data frame from several vectors in R.

There are two common ways to do so:

Method 1: Use data.frame

#create four vectors
team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B')
points=c(99, 68, 86, 88, 95, 74, 78, 93)
assists=c(22, 28, 31, 35, 34, 45, 28, 31)
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)

#create data frame from vectors
df <- data.frame(team, points, assists, rebounds)

Method 2: Use as.data.frame with cbind

#create four vectors
team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B')
points=c(99, 68, 86, 88, 95, 74, 78, 93)
assists=c(22, 28, 31, 35, 34, 45, 28, 31)
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)

#create data frame from vectors
df <- as.data.frame(cbind(team, points, assists, rebounds))

Both of these methods will produce the same result.

The following examples show how to use each method in practice.

Method 1: Create Data Frame from Vectors Using data.frame

The following code shows how to create a data frame in R named df by using four vectors as the individual columns in the data frame:

#create four vectors
team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B')
points=c(99, 68, 86, 88, 95, 74, 78, 93)
assists=c(22, 28, 31, 35, 34, 45, 28, 31)
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)

#create data frame from vectors
df <- data.frame(team, points, assists, rebounds)

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

The resulting data frame contains four columns in which each column is one of the vectors that we created.

Note that the columns will appear in the order that you specify them within the data.frame() function.

Method 2: Create Data Frame from Vectors Using as.data.frame() and cbind()

The following code shows how to create a data frame in R named df2 by using four vectors as the individual columns in the data frame:

#create four vectors
team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B')
points=c(99, 68, 86, 88, 95, 74, 78, 93)
assists=c(22, 28, 31, 35, 34, 45, 28, 31)
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)

#create data frame from vectors
df2 <- as.data.frame(cbind(team, points, assists, rebounds))

#view data frame
df2

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

The resulting data frame contains four columns in which each column is one of the vectors that we created.

Notice that this results in the exact same data frame as the previous example.

It’s worth noting that each vector that you use as a column in the data frame must have the same length or else you’ll receive an error when attempting to create the data frame.

For example, suppose our points vector has one value more than all of the other vectors:

frame:

#create four vectors
team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B')
points=c(99, 68, 86, 88, 95, 74, 78, 93, 88)
assists=c(22, 28, 31, 35, 34, 45, 28, 31)
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)

#attempt to create data frame from vectors
df2 <- as.data.frame(cbind(team, points, assists, rebounds))

Error in data.frame(team, points, assists, rebounds) : 
  arguments imply differing number of rows: 8, 9
Execution halted

We receive an error message that arguments imply different number of rows: 8, 9.

This tells us that some of the vectors contain 8 elements while some contain 9 elements.

All of the vectors must have the same number of elements in order for a data frame to be created.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Data Frame with Random Numbers in R
How to Create a Vector with Random Numbers in R
How to Create a Matrix with Random Numbers in R
How to Select Random Samples in R

Leave a Reply

Your email address will not be published. Required fields are marked *