
上QQ阅读APP看书,第一时间看更新
The unite() function
unite focuses on combining multiple columns into a single column. This is regarded as a convenience function that pastes multiple columns together.
The basic implementation syntax is as follows:
unite(data, col, ..., sep = "_", remove = TRUE)
Here, the parameters are as follows:
- data: Data frame.
- col: The name of the column to be added. Further specification of columns can be added.
- sep: Separator to use between the values.
- remove: If TRUE, this will remove input columns from the data frame mentioned.
In our dataset, where we need to combine Fuel Type columns and Drive versus Cylinder attributes to understand the fuel efficiency, we can use the unite function with a separator value to combine the columns:
> mpg4<- unite_(mpg, "FuelEfficiency", c("drv","fl")) > View(mpg4)
The output generated is as follows:

mpg4 is a new dataset created as follows:

FuelEfficiency is the column that combines both attributes with a separator, _.
We can combine multiple columns together as one in a similar fashion, with the main focus being to tidy up the data.