R Vector to data frame: How to Create DataFrame from Vector

A vector in R is a sequence of data items of the same data type. To create a vector, use the c() function. Data Frame in R is a table or two-dimensional array-like structure in which a row contains a set of values, and each column holds values of one variable. Each column in the data frame should comprise an equal number of the data components.

R Vector to data frame

To convert a vector to data frame, use the data.frame() method. The data.frame() method creates data frames, tightly coupled collections of variables. To create a data frame in R from the vector, we must first have a set of vectors containing data. 

Code snippet for vector to data frame

data.frame(vectors)

Here, we need some vector data to create a data frame. Let’s go step by step.

Step 1: Define four-vectors.

To create a vector, use the c() function in R. Let’s define four-vectors.

title <- c("Breaking Bad", "Stranger Things",
 "Money Heist", "Sherlock", "The Mandalorian")
year <- c(2008, 2016, 2017, 2010, 2019)
imdb <- c(9.5, 8.8, 8.4, 9.1, 8.8)

Each vector contains five components or elements.

Step 2: Create a data frame from vectors

Pass the vectors to the data.frame() function will create take title, year, and imdb as columns and elements or components as its rows.

title <- c("Breaking Bad", "Stranger Things",
 "Money Heist", "Sherlock", "The Mandalorian")
year <- c(2008, 2016, 2017, 2010, 2019)
imdb <- c(9.5, 8.8, 8.4, 9.1, 8.8)

df <- data.frame(title, year, imdb)
df

Output

   title           year   imdb
1  Breaking Bad    2008   9.5
2  Stranger Things 2016   8.8
3  Money Heist     2017   8.4
4  Sherlock        2010   9.1
5  The Mandalorian 2019   8.8

That is it. We got the tabular structured data frame with the help of vectors.

Here we have 3 vectors, The title, which is a character vector. The year is a numeric vector, and the imdb is also a numeric vector, and using them, we have created a data frame. Once a data frame is created we can apply various data frame operations.

To convert character vector to numeric vector, use the as.numeric() function.

How to get the structure of an R data frame

To get the structure of the R data frame, use the str() function.

title <- c("Breaking Bad", "Stranger Things",
 "Money Heist", "Sherlock", "The Mandalorian")
year <- c(2008, 2016, 2017, 2010, 2019)
imdb <- c(9.5, 8.8, 8.4, 9.1, 8.8)

df <- data.frame(title, year, imdb)
str(df)

Output

'data.frame': 5 obs. of 3 variables:
 $ title: chr "Breaking Bad" "Stranger Things" "Money Heist" "Sherlock" ...
 $ year : num 2008 2016 2017 2010 2019
 $ imdb : num 9.5 8.8 8.4 9.1 8.8

How to get a summary of Data in Data Frame

To get the summary of a data frame in R, use the summary() function.

title <- c("Breaking Bad", "Stranger Things",
 "Money Heist", "Sherlock", "The Mandalorian")
year <- c(2008, 2016, 2017, 2010, 2019)
imdb <- c(9.5, 8.8, 8.4, 9.1, 8.8)

df <- data.frame(title, year, imdb)
summary(df)

Output

 title                year        imdb
Length:5          Min. :2008    Min. :8.40
Class :character  1st Qu.:2010  1st Qu.:8.80
Mode :character   Median :2016  Median :8.80
Mean :2014                      Mean :8.92
3rd Qu.:2017                    3rd Qu.:9.10
Max. :2019                      Max. :9.50

Extract Data from Data Frame

To access a specific column from a data frame, use the name of the column.

title <- c("Breaking Bad", "Stranger Things",
 "Money Heist", "Sherlock", "The Mandalorian")
year <- c(2008, 2016, 2017, 2010, 2019)
imdb <- c(9.5, 8.8, 8.4, 9.1, 8.8)

df <- data.frame(title, year, imdb)
new_df <- data.frame(df$title, df$imdb)
new_df

Output

   df.title        df.imdb
1  Breaking Bad     9.5
2  Stranger Things  8.8
3  Money Heist      8.4
4  Sherlock         9.1
5  The Mandalorian  8.8

That is it for converting a vector to the data frame in R.

See also

R List to Vector

Add vectors in R

Vector math in R

Leave a Comment