• Post author:
  • Post category:R Programming
  • Post last modified:March 27, 2024
  • Reading time:11 mins read
You are currently viewing R-Create DataFrame with Column Names

How to create an R data frame with column names? There are several ways to achieve this in the R programming language, including using functions such as data.frame(), R matrix, and R list. In this article, I will explain how to create a data frame with column names using the following methods.

Advertisements
  • Create a data frame with column names using data.frame() function.
  • Initialize the data frame with column names using Matrix
  • Create an Empty DataFrame with Column Names
  • Create DataFrame Column Names from List.

1. Quick Examples of Creating DataFrame with Column Names in R

Following are quick examples of how to create an r data frame with column names.


# Below are the quick examples of creating data frame

# Example 1: Create DataFrame with column names 
df <- data.frame(
  id = c(10,11,12,13),
  name = c('sai','ram','deepika','sahithi'),
  dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))
)

# Example 2:  Create data frame with column names from matrix 
# Create mtarix with 4 columns
mat <- matrix(1:20, ncol = 4)

# Convert the data frame using as.data.farme()
df <- as.data.frame(mat) 


# Example 3: Rename the column names using names()
names(df) <- c('vec1', 'vec2', 'vec3', 'vec4')

# Example 4: Create a Empty DataFrame with 0 rows and n columns
# Create a Vector with Columns
columns = c("id","name","dob") 
df = data.frame(matrix(nrow = 0, ncol = length(columns))) 
# Assign column names
colnames(df) = columns

# Example 5: Create data frame with column names using list
# Create r list
# Convert list to data frame
list <- list(id = c(1 , 2, 3, 4, 5),
                       name = c('sai','ram','hari','deepak','sahithi'),
                       gender = c('m','m','m','m','f'))
# Combining the columns of list
df <- do.call(rbind, list)
# Convert matrix to data frame
df1 <- as.data.frame(df)

# Example 6: Reaname the column names of data frame
colnames(df1) <- c("Col1", "Col2", "Col3", "Col4", "Col5")

2. Create DataFrame with Column Names using data.frame() function

You can create data frame values and column names while creating using data.frame() function. this function returns the data frame with the column names ‘id‘, ‘name’, and ‘dob’. In the R columns are referred to as vectors and you can specify the column values using vectors.


# Create DataFrame with column names
df <- data.frame(
  id = c(10,11,12,13),
  name = c('sai','ram','deepika','sahithi'),
  dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))
)

# Print DataFrame
df

Yields below output.

r Create DataFrame with Column Names

3. Create DataFrame with Column Names using Matrix

Alternatively, you can use the matrix to create a data frame with column names in R language. Before going to create a data frame from the matrix we need to create the matrix with specified columns using the R matrix() function. Then you can convert it into a data frame.


# Create mtarix with 4 columns
mat <- matrix(1:20, ncol = 4)
mat

Yields below output.

r Create DataFrame with Column Names

As you can see, the above code has returned the matrix object without having any column names.

To apply the as.data.frame() function to the above object to convert its class into a data frame class with the column names. Let’s pass the matrix object into the as.data.frame() function to convert the data frame object with default column names.


# Create data frame with column names from matrix
# Convert the data frame using as.data.farme()
df <- as.data.frame(mat)
df

Yields below output.

r Create DataFrame with Column Names

4. Rename the DataFrame Column Names

To rename the column names in R data frame you can use the names()method that can be used to rename all column names (list with column names). You can also use this method to rename dataframe column by index in R. For example,


# Rename the column names using names()
names(df) <- c('vec1', 'vec2', 'vec3', 'vec4')
df

# Output:
#   vec1 vec2 vec3 vec4
# 1    1    6   11   16
# 2    2    7   12   17
# 3    3    8   13   18
# 4    4    9   14   19
# 5    5   10   15   20

5. Create an Empty DataFrame with Column Names

To create an empty data frame with specified column names in R, you can use the data.frame() function. First, we need column names on DataFrame, let’s create column names using a vector.

Note: In R language c() is used to create a Vector.

Now, let’s create an empty DataFrame by using the data.frame() function. Finally, print the DataFrame.


# Create a Vector with Columns
columns = c("id","name","dob") 

# Create a Empty DataFrame with 0 rows and n columns
df = data.frame(matrix(nrow = 0, ncol = length(columns))) 

# Assign column names
colnames(df) = columns

# print DataFrame
print(df)

# Output:
# [1] id   name dob 
# <0 rows> (or 0-length row.names)

As you see above, our DataFrame has 0 rows with 3 columns id, name and dob.

6. Create DataFrame Column Names from List

Alternatively, use the list to create a data frame with column names in R. This process is done in three steps. First, create a list named list with three elements 'id', 'name', and 'gender'. Each element is a vector containing values for different individuals. Then use the do.call function to combine the columns of the list using rbind (row bind).

The as.data.frame function is used to convert the matrix df into a data frame named df1. Now, df1 is a data frame where each column represents a variable (‘id’, ‘name’, ‘gender’), and each row represents an individual.


# Create data frame with column names using list
# Create r list
# Convert list to data frame
list <- list(id = c(1 , 2, 3, 4, 5),
                       name = c('sai','ram','hari','deepak','sahithi'),
                       gender = c('m','m','m','m','f'))
print(list)

# Combining the columns of list
df <- do.call(rbind, list)

# Convert matrix to data frame
df1 <- as.data.frame(df)
print (df1)

Yields below output.


# Output:
# Create list
$id
[1] 1 2 3 4 5

$name
[1] "sai"     "ram"     "hari"    "deepak"  "sahithi"

$gender
[1] "m" "m" "m" "m" "f"

# Convert list to dataframe

        V1  V2   V3     V4      V5
id       1   2    3      4       5
name   sai ram hari deepak sahithi
gender   m   m    m      m       f

To rename the column names of the above data frame you can use the colnames() function from R base. First, specify the new column names using the vector and assign them to the data frame using colnames(). Finally, get the new data frame with modified column names.


# Reaname the column names of data frame
colnames(df1) <- c("Col1", "Col2", "Col3", "Col4", "Col5")
df1

# Output:
#        Col1 Col2 Col3   Col4    Col5
# id        1    2    3      4       5
# name    sai  ram hari deepak sahithi
# gender    m    m    m      m       f

Conclusion

In this article, I have explained how to create the data frame with column names in r using data.frame() function, matrix, and list. Also explained the creation of an empty data frame with column names and how to rename the column names using various functions of the R language.

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.