- R Data Visualization Cookbook
- Atmajitsinh Gohil
- 236字
- 2021-08-06 19:21:07
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")
- Embedded Linux Projects Using Yocto Project Cookbook
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- 零基礎(chǔ)學(xué)Visual C++第3版
- 微服務(wù)設(shè)計原理與架構(gòu)
- C++ 從入門到項目實踐(超值版)
- The HTML and CSS Workshop
- Java Web開發(fā)就該這樣學(xué)
- Spring MVC+MyBatis開發(fā)從入門到項目實踐(超值版)
- UML2面向?qū)ο蠓治雠c設(shè)計(第2版)
- 詩意的邊緣
- Cocos2D Game Development Essentials
- Delphi Cookbook
- Visual C#網(wǎng)絡(luò)編程
- HTML5+CSS3+JavaScript從入門到精通(微課精編版)
- C#典型模塊與項目實戰(zhàn)大全