Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
Data Into Insights (Spring 2021)/R4DS Chapter 12
(section)
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 12.3 == 1. Why are pivot_longer() and pivot_wider() not perfectly symmetrical? Carefully consider the following example: <syntaxhighlight lang="R"> stocks <- tibble( year = c(2015, 2015, 2016, 2016), half = c( 1, 2, 1, 2), return = c(1.88, 0.59, 0.92, 0.17) ) stocks %>% pivot_wider(names_from = year, values_from = return) %>% pivot_longer(`2015`:`2016`, names_to = "year", values_to = "return") </syntaxhighlight> (Hint: look at the variable types and think about column names.) 2. Why does this code fail? <syntaxhighlight lang="R"> table4a %>% pivot_longer(c(1999, 2000), names_to = "year", values_to = "cases") #> Error: Can't subset columns that don't exist. #> β Locations 1999 and 2000 don't exist. #> βΉ There are only 3 columns. </syntaxhighlight> 3. What would happen if you widen this table? Why? How could you add a new column to uniquely identify each value? <syntaxhighlight lang="R"> people <- tribble( ~name, ~variable, ~value, #-----------------|--------|------ "Phillip Woods", "age", 45, "Phillip Woods", "height", 186, "Phillip Woods", "age", 50, "Jessica Cordero", "age", 37, "Jessica Cordero", "height", 156 ) </syntaxhighlight> 4. Tidy the simple tibble below. Do you need to make it wider or longer? What are the variables? <syntaxhighlight lang="R"> preg <- tribble( ~pregnant, ~m, ~f, "yes", NA, 10, "no", 20, 12 ) </syntaxhighlight>
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information