Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
R Data Visualization Cookbook

You're reading from  R Data Visualization Cookbook

Product type Book
Published in Jan 2015
Publisher
ISBN-13 9781783989508
Pages 236 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

R Data Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. A Simple Guide to R 2. Basic and Interactive Plots 3. Heat Maps and Dendrograms 4. Maps 5. The Pie Chart and Its Alternatives 6. Adding the Third Dimension 7. Data in Higher Dimensions 8. Visualizing Continuous Data 9. Visualizing Text and XKCD-style Plots 10. Creating Applications in R Index

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 the negative sign before the column or row to be deleted, as follows:

data = data[,-2]

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:

data = data.frame(x = c(1:4), y = c("tom","jerry","luke","brian"))
data = data[c(2,1)]# will reorder the columns
data

To view the column names of a data frame, we can use the names() function:

names(data)

To rename our column names, we can use the colnames() function:

colnames(data) = c("Number","Names")
You have been reading a chapter from
R Data Visualization Cookbook
Published in: Jan 2015 Publisher: ISBN-13: 9781783989508
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}