R-Programming 1.a) There are Several tidyr Functions which haven’t been discusse
ID: 3887039 • Letter: R
Question
R-Programming
1.a) There are Several tidyr Functions which haven’t been discussed yet. Pick one of these Functions and Create a Simple Example using that Function
Functions are detailed in:https://cran.r-project.org/web/packages/tidyr/tidyr.pdf
1.b)
R-Programming
Create the gather <-> spread inverse example Without using the rename function from the Image Below.
In the separate <-> unite inverse example, Fill out the rest of the arguments for the spread() function. Example is found in the Picture Below
inverse gather() and spread( Year Month Day Element Temp 12015 1 1 tmax 78 2 2015 tmin 72 3 2015 2 2 tmax 82 4 2015 2 2 tmin 74 5 2015 4 4 tmax 81 62015 4 4 tmin 71 72015 6 3 tmax 80 82015 6 3 tmin 71 spread(myData, Element, Temp) Year Month Day tmax tmin 1 2015 78 72 2 2015 2 2 82 74 3 2015 4 4 8171 4 2015 63 80 71 myData %5% gather( Element" "Temp",-Year,-Month, Day) %5% 0 70 >0 rename(c(variable- "Element",value-"TemExplanation / Answer
1a) gather function : It takes multiple columns of a data frame and collapses into key-value pairs, duplicating all other columns as needed. For example :
Program
library(tidyr)
student <-data.frame(
name = c("Roger","Andy","Roger","Andy"),
subject = c("English","English","Maths","Maths"),
marks = c("50","85","75","90"),
grade = c ("D","B","C","A")
)
student
student %>% gather(key, value, marks:grade)
Output :
> student
name subject marks grade
1 Roger English 50 D
2 Andy English 85 B
3 Roger Maths 75 C
4 Andy Maths 90 A
> student %>% gather(key, value, marks:grade)
name subject key value
1 Roger English marks 50
2 Andy English marks 85
3 Roger Maths marks 75
4 Andy Maths marks 90
5 Roger English grade D
6 Andy English grade B
7 Roger Maths grade C
8 Andy Maths grade A
In above exampe, for student data fram, we have performed gather operation by which columns(variables) marks & grade becomes key and their value becomes values in final table, while other values of columns are replicated to achieve our objective. Its just another representation of data to perform some operation effiiciently.
2) Original data
> myData <-data.frame(
+ Year = c(2015,2015,2015,2015),
+ Month = c(1,2,4,6),
+ Day = c(1,2,4,3),
+ tmax = c (78,82,81,80),
+ tmin = c(72,74,71,71)
+ )
myData
Year Month Day tmax tmin
1 2015 1 1 78 72
2 2015 2 2 82 74
3 2015 4 4 81 71
4 2015 6 3 80 71
Gather Example
> myData %>% gather(Element, Temp, tmax:tmin)
Year Month Day Element Temp
1 2015 1 1 tmax 78
2 2015 2 2 tmax 82
3 2015 4 4 tmax 81
4 2015 6 3 tmax 80
5 2015 1 1 tmin 72
6 2015 2 2 tmin 74
7 2015 4 4 tmin 71
8 2015 6 3 tmin 71
Spread Example
> gatherData <- myData %>% gather(Element, Temp, tmax:tmin)
> gatherData %>% spread(Element, Temp)
Year Month Day tmax tmin
1 2015 1 1 78 72
2 2015 2 2 82 74
3 2015 4 4 81 71
4 2015 6 3 80 71
Seprate <-> Unite Example
Original Data
library(tidyr)
myData <-data.frame(
Name = c("Mary","John","Sandy", "Alex"),
Year = c(2011,2012,2016,2015),
Month = c(1,4,12,10),
Day = c (14,7,6,2)
)
myData
Name Year Month Day
1 Mary 2011 1 14
2 John 2012 4 7
3 Sandy 2016 12 6
4 Alex 2015 10 2
Unite Example
> myData <- myData %>% unite("Date", Year, Month, Day, sep="/")
> myData
Name Date
1 Mary 2011/1/14
2 John 2012/4/7
3 Sandy 2016/12/6
4 Alex 2015/10/2
Separate Example
> separate(myData, Date, c("Year", "Month", "Day"), sep="/")
Name Year Month Day
1 Mary 2011 1 14
2 John 2012 4 7
3 Sandy 2016 12 6
4 Alex 2015 10 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.