R – Create Data Frame with Column Names

In this tutorial, you will learn how to create a Data Frame with column names in R language.

We shall cover scenarios where we need to 1. Create a Data Frame with specific column names using data.frame(), 2. create an empty Data Frame and then set column names to the Data Frame, or 3. create a Data Frame and then set column names.

1. Create Data Frame with column names using data.frame() in R

We can create a Data Frame by passing vectors to data.frame(). While passing the vectors to data.frame(), we can specify the column names as well.

Steps

In the following program, we create a Data Frame from three vectors. These vectors will be used as columns. Each vector is specified in the form tag = value. tag would be considered as column name, and the value would be considered as column values.

Program

R program to create Data Frame with column names using data.frame().

example.R

</>
Copy
# Create Data Frame from vectors
df <- data.frame(a = c(10, 20, 30, 40),
                 b = c(15, 25, 35, 45),
                 c = c(18, 28, 38, 48))

print(df)

Here the tag names of the vectors: a, b, and c shall be used as column names in the Data Frame.

Output

   a  b  c
1 10 15 18
2 20 25 28
3 30 35 38
4 40 45 48

2. Create empty Data Frame with column names using colnames() in R

We can create an empty Data Frame with specific column names using colnames().

Steps

  1. Given a list of column names as a vector in column_names.
  2. Create an empty Data Frame. Call data.frame() and pass a matrix with number of rows as zero, and number of columns from the given column_names vector. Assign the empty Data Frame to df.
  3. Set column names for the Data Frame df using colnames().

In the following program, we take a vector with three column names, create an empty Data Frame with zero rows and three columns, and set the column names to the Data Frame using colnames().

Program

R program to create an empty Data Frame with column names using colnames().

example.R

</>
Copy
# Given column names
column_names = c('a', 'b', 'c')

# Create empty Data Frame
df <- data.frame(matrix(nrow = 0, ncol = length(column_names)))

# Set column names to the Data Frame
colnames(df) = c('a', 'b', 'c')

print(df)

Output

[1] a b c
<0 rows> (or 0-length row.names)

3. Create Data Frame using vectors without column names and then set column names in R

We can create a Data Frame without explicitly specifying column names during creation of the Data Frame, but assign the column names to the Data Frame later.

Steps

  1. Create a Data Frame using vectors in df.
  2. Given a list of column names as a vector in column_names.
  3. Assign column_names vector to the colnames of the Data Frame df.
  4. The Data Frame df shall be set with the specified column names. Make sure that the number of column names is same as the number of columns in the Data Frame.

In the following program, we take a Data Frame df with three columns where column names are not specified. If column names are not specified, then R implicitly assigns some unique column names to the Data Frame. But we do not want these automatically created column names. We shall assign new column names given in column_names vector to the Data Frame using colnames().

Program

R program to create a Data Frame without specific column names, and then at a later point, we shall assign specific column names to the Data Frame using colnames().

example.R

</>
Copy
# Create Data Frame from vectors
df <- data.frame(c(10, 20, 30, 40),
                 c(15, 25, 35, 45),
                 c(18, 28, 38, 48))

cat("Data Frame before setting specific column names\n")
print(df)

# Set column names to the Data Frame
colnames(df) = c('a', 'b', 'c')

cat("\nData Frame before setting column names\n")
print(df)

Output

Data Frame before setting specific column names
  c.10..20..30..40. c.15..25..35..45. c.18..28..38..48.
1                10                15                18
2                20                25                28
3                30                35                38
4                40                45                48

Data Frame before setting column names
   a  b  c
1 10 15 18
2 20 25 28
3 30 35 38
4 40 45 48

Summary

In this tutorial, we learned how to create a Data Frame with column names in R programming language, covering different scenarios based on how Data Frame is created, or based on specifying column names implicitly or explicitly, with well detailed steps and programs.