R Basic

How to Create a Data Frame from Vectors in R

To create a data frame from vectors in R, use the data.frame() function and pass the vectors. Ensure you have the same length of all the vectors; otherwise, it throws an error.

The above figure shows five vectors. Each vector has its own type and the same length, which is five. After converting vectors to a data frame, each vector becomes a column of the data frame.

After the data frame has been created, each column retains its data type.

You can think of a vector as a series of values in a single column. It contains the same data type. Each column of a data frame is a vector. Therefore, if I have several vectors, I can combine them to create a data frame.

Basic Data Frame Creation

Let’s define the five vectors, which will act as columns of the data frame, and then pass them to the data.frame() method.

title <- c("Inception", "The Dark Knight", "Interstellar", "The Matrix", "Gladiator")
genre <- c("Sci-Fi", "Action", "Sci-Fi", "Sci-Fi", "Drama")
year <- c(2010, 2008, 2014, 1999, 2000)
rating <- c(8.8, 9.0, 8.6, 8.7, 8.5)
runtime <- c(148, 152, 169, 136, 155)


# Create a data frame from vectors
df <- data.frame(title, genre, year, rating, runtime)

print(df)

Output

Displaying the internal structure of the data frame

If you want to implement the internal structure of the data frame, you can use the “str()” function and pass the data frame to it.

title <- c("Inception", "The Dark Knight", "Interstellar", "The Matrix", "Gladiator")
genre <- c("Sci-Fi", "Action", "Sci-Fi", "Sci-Fi", "Drama")
year <- c(2010, 2008, 2014, 1999, 2000)
rating <- c(8.8, 9.0, 8.6, 8.7, 8.5)
runtime <- c(148, 152, 169, 136, 155)


# Create a data frame from vectors
df <- data.frame(title, genre, year, rating, runtime)

# Printing data frame structure
str(df)

Output

The above screenshot shows that title, genre have character columns, and year, rating, runtime have numeric columns.

Adding a new column to an existing data frame

To add a column to an existing data frame, you can use the “$” operator and assign values to the new column.

Let’s add the “director” column to our data frame.

title <- c("Inception", "The Dark Knight", "Interstellar", "The Matrix", "Gladiator")
genre <- c("Sci-Fi", "Action", "Sci-Fi", "Sci-Fi", "Drama")
year <- c(2010, 2008, 2014, 1999, 2000)
rating <- c(8.8, 9.0, 8.6, 8.7, 8.5)
runtime <- c(148, 152, 169, 136, 155)


# Create a data frame from vectors
df <- data.frame(title, genre, year, rating, runtime)

# Adding a new column to an existing data Frame
df$director <- c("C Nolan", "C Nolan", "C Nolan", "L Wachowski", "R Scott")
print(df)

Output

Adding a new row to an existing data frame

You can add a new row to an existing data frame using the rbind() function. It accepts the first argument as an existing data frame and a vector that will be converted into a new row in the data frame.

title <- c("Inception", "The Dark Knight", "Interstellar", "The Matrix", "Gladiator")
genre <- c("Sci-Fi", "Action", "Sci-Fi", "Sci-Fi", "Drama")
year <- c(2010, 2008, 2014, 1999, 2000)
rating <- c(8.8, 9.0, 8.6, 8.7, 8.5)
runtime <- c(148, 152, 169, 136, 155)

# Create a data frame from vectors
df <- data.frame(title, genre, year, rating, runtime)

# Adding a row to an existing data frame
df <- rbind(df, c("Kaun", "Horror", 1999, 7.8, 134))
print(df)

Output

That’s it!

Recent Posts

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

1 day ago

rowMeans(): Calculating the Mean of rows of a Data Frame in R

The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…

4 days ago

colSums(): Calculating the Sum of Columns of a Data Frame in R

The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…

1 week ago

rowSums(): Calculating the Sum of Rows of a Matrix or Data Frame in R

The rowSums() function calculates the sum of values in each numeric row of a matrix,…

2 weeks ago

R View() Function

The View() is a utility function in R that invokes a more intuitive spreadsheet-style data…

3 weeks ago

summary() Function: Producing Summary Statistics in R

The summary() is a generic function that produces the summary statistics for various R objects,…

4 weeks ago