We can add or extract any column of a data frame using the dollar ($) symbol, as depicted in the following code:
In the preceding example, we have added a new column called age
using the $
operator. Alternatively, we can also add columns and rows using the rbind()
and cbind()
functions in R as follows:
The cbind
and rbind
functions can also be used to add columns or rows to an existing matrix.
To remove a column or a row from a matrix or data frame, we can simply use the negative sign before the column or row to be deleted, as follows:
The data[,-2]
line will delete the second column from our data.
To re-order the columns of a data frame, we can type the following lines in the R command window:
To view the column names of a data frame, we can use the names()
function:
To rename our column names, we can use the colnames()
function: