Explain the tidyr() function in R along with installation.

359    Asked by bhagwatidubey in Data Science , Asked on Nov 9, 2019
Answered by bhagwati dubey

tidyr() is a complimentary package in R used to create tidy datasets.

Tidy data is when we have a data set where every row is an observation and every column is a variable, this way the data is organized in such a way where every cell is a value for a specific variable of a specific observation. Having data in this format will help build an understanding of our data and allow us to analyze or visualize it quickly and efficiently.

tidyr() can be installed and read in following way

install.packages('tidyr',repos='http://cran.us.r-project.org')

library(tidyr)

library(data.table)

Data.table is similar to data.frame but it provides different and clean syntax compared to data.frame

tidyr() covers the following functions

a)gather()- The gather() function will collapse multiple columns into key-pair values. The data frame above is considered wide since the time variable (represented as quarters) is structured such that each quarter represents a variable.

b) Spread()- It is a complement of gather() and is much flexible

c) separate() and unite()- Given either regular expression or a vector of character positions, separate() turns a single character column into multiple columns.

Unite is a convenience function to paste together multiple columns into one.



Your Answer

Interviews

Parent Categories