- Hands-On Exploratory Data Analysis with R
- Radhika Datar Harish Garg
- 236字
- 2021-06-24 14:10:43
The gather() function
There are times when our data is considered raw and unstacked (not in chronological order) and a common attribute of concern is used across the columns. To reformat the data so that these common attributes take up a single variable, the gather () function will take multiple columns and break them into key-value pairs, duplicating all other columns if needed.
The following illustration will help us to better understand the implementation of gather() function. The syntax for implementing the gather() function is as follows:
gather(data, key, value, ..., na.rm = FALSE, convert = FALSE)
Here, the parameters of the function are as follows:
- data: Data frame
- key: Name of the key
- value: Name of the value
- na.rm: If TRUE, it will remove rows from the output
- convert: If TRUE, it will automatically convert the specified key column
Suppose we need to gather information relating to the manufacturer and model and display other attributes in same way. In this case, there is a need to present only manufacturers and models in a systematic manner. We can achieve this with the help of the gather() function, demonstrated as follows:
> mpg2 <- mpg %>% gather(mpg, "Year of Establishment", "year", -manufacturer) > View(mpg2)
The output generated is displayed as follows:

It is clearly visible that the key-value pair is generated for the year of establishment of each and every model included in the dataset.
- Learning Microsoft Azure Storage
- 計算機應用基礎·基礎模塊
- 網頁編程技術
- 走入IBM小型機世界
- PIC單片機C語言非常入門與視頻演練
- Visual Basic從初學到精通
- B2B2C網上商城開發指南
- AWS Certified SysOps Administrator:Associate Guide
- 3D Printing for Architects with MakerBot
- Apache Superset Quick Start Guide
- 工業控制系統測試與評價技術
- MCGS嵌入版組態軟件應用教程
- 教育機器人的風口:全球發展現狀及趨勢
- 在實戰中成長:Windows Forms開發之路
- 無人駕駛感知智能