官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 平塘县| 夏河县| 霍城县| 广汉市| 依安县| 娄底市| 油尖旺区| 乌兰察布市| 汉中市| 乌拉特中旗| 固原市| 胶州市| 冷水江市| 原平市| 铜梁县| 东明县| 尤溪县| 潮州市| 嘉鱼县| 大洼县| 台南县| 长子县| 威海市| 南投市| 名山县| 乌鲁木齐市| 绩溪县| 南平市| 儋州市| 望谟县| 高碑店市| 玛纳斯县| 永德县| 碌曲县| 利川市| 安西县| 斗六市| 汶上县| 津市市| 兴国县| 怀来县|