In this tutorial, we will learn how to add a column to a Dataframe in R programming.

To add a column to an R Dataframe, we will use dollar sign $ as shown in the following syntax.

where

  • mydataframe is the dataframe to which we shall add a new column.
  • newcolumn is the name of the new column.
  • vector_name is the vector containing the values of new column.

Example – Add Column to R Dataframe

In this example, we will create a dataframe DF1 and add a column to the dataframe.

Column V4 is successfully added to the dataframe.

Example – Add Column (with lesser length) to R Dataframe

In this example, we will create a dataframe DF1 and add a column with number of rows less than the existing number of rows in the dataframe.

You will get an error “replacement has 4rows, data has 5”.