How to Make a Data Frame with Column Names in R (2 Examples)

On this page, I’ll demonstrate how to make a data frame with column names in the R programming language.

Example 1: Constructing a Data Frame with Column Names & Values

df_A <- data.frame(col1 = 1:6,          # Data frame with values
                   col2 = "yyy",
                   col3 = LETTERS[10:15],
                   col4 = 55)
df_A
#   col1 col2 col3 col4
# 1    1  yyy    J   55
# 2    2  yyy    K   55
# 3    3  yyy    L   55
# 4    4  yyy    M   55
# 5    5  yyy    N   55
# 6    6  yyy    O   55

Example 2: Constructing an Empty Data Frame with Column Names

df_B <- data.frame(col1 = numeric(),    # Data frame without values
                   col2 = factor(),
                   col3 = integer(),
                   col4 = character())
df_B
# [1] col1 col2 col3 col4
# <0 rows> (or 0-length row.names)

Related Tutorials

You may have a look at the following R programming tutorials. They illustrate topics such as variables, indices, and matrices.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top