Editing a data frame in R

Once we have generated a data and converted it into a data frame, we can edit any row or column of a data frame.

How to do it...

We can add or extract any column of a data frame using the dollar ($) symbol, as depicted in the following code:

data = data.frame(x = c(1:4), y = c("tom","jerry","luke","brian"))
data$age = c(2,2,3,5)
data

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:

age = c(2,2,3,5)
data = cbind(data, age)

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 ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.