Variable<-c("Population for whom poverty status is determined", "AGE", "Under 18 years", "Related children under 18 years", "18 to 64 years", "65 years and over", "SEX", "Male", "Female", "RACE AND HISPANIC OR LATINO ORIGIN", "One race", "White", "Black or African American", "American Indian and Alaska Native", "Asian", "Native Hawaiian and Other Pacific Islander", "Some other race", "Two or more races", "Hispanic or Latino origin (of any race)", "White alone, not Hispanic or Latino", "EDUCATIONAL ATTAINMENT", "Population 25 years and over", "Less than high school graduate", "High school graduate (includes equivalency)", "Some college, associate's degree", "Bachelor's degree or higher", "EMPLOYMENT STATUS", "Civilian labor force 16 years and over", "Employed", "Male", "Female", "Unemployed", "Male", "Female", "WORK EXPERIENCE", "Population 16 years and over", "Worked full-time, year-round in the past 12 months", "Worked part-time or part-year in the past 12 months", "Did not work", "All Individuals below:", "50 percent of poverty level", "125 percent of poverty level", "150 percent of poverty level", "185 percent of poverty level", "200 percent of poverty level", "Unrelated individuals for whom poverty status is determined", "Male", "Female", "Mean income deficit for unrelated individuals (dollars)", "Worked full-time, year-round in the past 12 months", "Worked less than full-time, year-round in the past 12 months", "Did not work", "PERCENT IMPUTED")

Year<-rep(2010,each=53)
County<-rep("San Diego", each=53)
State<-rep("California", each=53)
Total<-sample(1:100, 53, replace=TRUE)
Below_poverty_level<-sample(1:100, 53, replace=TRUE)
Percent_below_poverty_level<-sample(1:100, 53, replace=TRUE)

poverty<-data.frame(Variable, Year, County, State, Total,Below_poverty_level, Percent_below_poverty_level )

poverty
##                                                        Variable Year    County
## 1              Population for whom poverty status is determined 2010 San Diego
## 2                                                           AGE 2010 San Diego
## 3                                                Under 18 years 2010 San Diego
## 4                               Related children under 18 years 2010 San Diego
## 5                                                18 to 64 years 2010 San Diego
## 6                                             65 years and over 2010 San Diego
## 7                                                           SEX 2010 San Diego
## 8                                                          Male 2010 San Diego
## 9                                                        Female 2010 San Diego
## 10                           RACE AND HISPANIC OR LATINO ORIGIN 2010 San Diego
## 11                                                     One race 2010 San Diego
## 12                                                        White 2010 San Diego
## 13                                    Black or African American 2010 San Diego
## 14                            American Indian and Alaska Native 2010 San Diego
## 15                                                        Asian 2010 San Diego
## 16                   Native Hawaiian and Other Pacific Islander 2010 San Diego
## 17                                              Some other race 2010 San Diego
## 18                                            Two or more races 2010 San Diego
## 19                      Hispanic or Latino origin (of any race) 2010 San Diego
## 20                          White alone, not Hispanic or Latino 2010 San Diego
## 21                                       EDUCATIONAL ATTAINMENT 2010 San Diego
## 22                                 Population 25 years and over 2010 San Diego
## 23                               Less than high school graduate 2010 San Diego
## 24                  High school graduate (includes equivalency) 2010 San Diego
## 25                             Some college, associate's degree 2010 San Diego
## 26                                  Bachelor's degree or higher 2010 San Diego
## 27                                            EMPLOYMENT STATUS 2010 San Diego
## 28                       Civilian labor force 16 years and over 2010 San Diego
## 29                                                     Employed 2010 San Diego
## 30                                                         Male 2010 San Diego
## 31                                                       Female 2010 San Diego
## 32                                                   Unemployed 2010 San Diego
## 33                                                         Male 2010 San Diego
## 34                                                       Female 2010 San Diego
## 35                                              WORK EXPERIENCE 2010 San Diego
## 36                                 Population 16 years and over 2010 San Diego
## 37           Worked full-time, year-round in the past 12 months 2010 San Diego
## 38          Worked part-time or part-year in the past 12 months 2010 San Diego
## 39                                                 Did not work 2010 San Diego
## 40                                       All Individuals below: 2010 San Diego
## 41                                  50 percent of poverty level 2010 San Diego
## 42                                 125 percent of poverty level 2010 San Diego
## 43                                 150 percent of poverty level 2010 San Diego
## 44                                 185 percent of poverty level 2010 San Diego
## 45                                 200 percent of poverty level 2010 San Diego
## 46  Unrelated individuals for whom poverty status is determined 2010 San Diego
## 47                                                         Male 2010 San Diego
## 48                                                       Female 2010 San Diego
## 49      Mean income deficit for unrelated individuals (dollars) 2010 San Diego
## 50           Worked full-time, year-round in the past 12 months 2010 San Diego
## 51 Worked less than full-time, year-round in the past 12 months 2010 San Diego
## 52                                                 Did not work 2010 San Diego
## 53                                              PERCENT IMPUTED 2010 San Diego
##         State Total Below_poverty_level Percent_below_poverty_level
## 1  California    53                  73                          51
## 2  California    62                  32                          46
## 3  California    76                  48                          58
## 4  California    62                  29                          11
## 5  California    40                  82                          63
## 6  California    54                  21                          72
## 7  California    42                  87                          79
## 8  California    55                  48                          70
## 9  California    84                  12                          49
## 10 California    40                  59                          35
## 11 California    32                  38                          60
## 12 California    89                 100                          39
## 13 California    80                  73                           6
## 14 California    59                  56                          38
## 15 California    71                  85                           8
## 16 California     5                  27                          97
## 17 California     6                  67                           3
## 18 California    13                  73                          62
## 19 California    70                  67                          12
## 20 California    68                  16                          28
## 21 California    87                   9                          54
## 22 California    98                  96                          54
## 23 California    81                  97                          65
## 24 California    85                  20                           7
## 25 California     4                  93                          81
## 26 California    45                  88                         100
## 27 California     9                  84                          62
## 28 California    26                  25                          34
## 29 California    81                  62                          37
## 30 California     3                  24                          18
## 31 California    37                  18                          93
## 32 California    91                  33                          20
## 33 California    55                  26                          20
## 34 California    59                  65                          60
## 35 California    32                   5                          81
## 36 California   100                  61                          40
## 37 California    58                  95                          72
## 38 California    95                  87                          66
## 39 California     3                  56                          54
## 40 California    61                  86                          96
## 41 California    62                  51                          31
## 42 California    83                  22                          25
## 43 California    98                  78                          36
## 44 California    77                  43                          83
## 45 California    85                  43                          49
## 46 California    40                  62                          49
## 47 California    30                  84                          51
## 48 California    48                  49                          28
## 49 California    50                  56                          67
## 50 California    55                  41                           5
## 51 California    70                   6                          25
## 52 California    48                  80                         100
## 53 California    86                  26                           6
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
newdata <- poverty[ which(poverty$Variable=='Population 25 years and over'|poverty$Variable=='Less than high school graduate' |poverty$Variable=="High school graduate (includes equivalency)" | poverty$Variable=="Some college, associate's degree"|poverty$Variable=="Bachelor's degree or higher" ), ]

#inspect
table(newdata$Variable) #making sure all varaibles selected are included
## 
##                 Bachelor's degree or higher 
##                                           1 
## High school graduate (includes equivalency) 
##                                           1 
##              Less than high school graduate 
##                                           1 
##                Population 25 years and over 
##                                           1 
##            Some college, associate's degree 
##                                           1
head(newdata)
##                                       Variable Year    County      State Total
## 22                Population 25 years and over 2010 San Diego California    98
## 23              Less than high school graduate 2010 San Diego California    81
## 24 High school graduate (includes equivalency) 2010 San Diego California    85
## 25            Some college, associate's degree 2010 San Diego California     4
## 26                 Bachelor's degree or higher 2010 San Diego California    45
##    Below_poverty_level Percent_below_poverty_level
## 22                  96                          54
## 23                  97                          65
## 24                  20                           7
## 25                  93                          81
## 26                  88                         100
colnames(newdata)
## [1] "Variable"                    "Year"                       
## [3] "County"                      "State"                      
## [5] "Total"                       "Below_poverty_level"        
## [7] "Percent_below_poverty_level"
#Transform
edu<-newdata %>% 
  gather(variable, value, -(Variable:State)) %>% 
  unite(temp, Variable, variable) %>%
  spread(temp, value)

head(edu)
##   Year    County      State Bachelor's degree or higher_Below_poverty_level
## 1 2010 San Diego California                                              88
##   Bachelor's degree or higher_Percent_below_poverty_level
## 1                                                     100
##   Bachelor's degree or higher_Total
## 1                                45
##   High school graduate (includes equivalency)_Below_poverty_level
## 1                                                              20
##   High school graduate (includes equivalency)_Percent_below_poverty_level
## 1                                                                       7
##   High school graduate (includes equivalency)_Total
## 1                                                85
##   Less than high school graduate_Below_poverty_level
## 1                                                 97
##   Less than high school graduate_Percent_below_poverty_level
## 1                                                         65
##   Less than high school graduate_Total
## 1                                   81
##   Population 25 years and over_Below_poverty_level
## 1                                               96
##   Population 25 years and over_Percent_below_poverty_level
## 1                                                       54
##   Population 25 years and over_Total
## 1                                 98
##   Some college, associate's degree_Below_poverty_level
## 1                                                   93
##   Some college, associate's degree_Percent_below_poverty_level
## 1                                                           81
##   Some college, associate's degree_Total
## 1                                      4