Converting wide tables into long tables

In order to keep only the Region and Name column and convert everything else into a key-value pair, run:

gathered <- gather(tstate, "Keys", "Values", -Region, -Name) %>% arrange(Region) # Note that arrange has been used only for aesthetic purposes 
 
gathered 
 
# # A tibble: 400 x 4 
# Region          Name       Keys Values 
# <fctr>         <chr>      <chr>  <dbl> 
#   1 Northeast   Connecticut Population   3100 
# 2 Northeast         Maine Population   1058 
# 3 Northeast Massachusetts Population   5814 
# 4 Northeast New Hampshire Population    812 
# 5 Northeast    New Jersey Population   7333 
 

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.