- 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.
- 三菱FX3U/5U PLC從入門到精通
- 21小時(shí)學(xué)通AutoCAD
- 程序設(shè)計(jì)語言與編譯
- Dreamweaver CS3網(wǎng)頁設(shè)計(jì)與網(wǎng)站建設(shè)詳解
- 數(shù)據(jù)運(yùn)營之路:掘金數(shù)據(jù)化時(shí)代
- Photoshop CS3特效處理融會貫通
- Implementing Oracle API Platform Cloud Service
- INSTANT Autodesk Revit 2013 Customization with .NET How-to
- The Python Workshop
- 精通數(shù)據(jù)科學(xué):從線性回歸到深度學(xué)習(xí)
- 啊哈C!思考快你一步
- Salesforce Advanced Administrator Certification Guide
- Cloud Security Automation
- Unreal Development Kit Game Design Cookbook
- 從機(jī)器學(xué)習(xí)到無人駕駛