Determining type
- eighties has the year variable stores as a factor
- sevnties has it stored as a character
- when combined it will be a character
- we will get a warning message
## # A tibble: 10 x 3
## year album band
## <fctr> <chr> <chr>
## 1 1970 Bridge Over Troubled Water Simon and Garfunkel
## 2 1971 Jesus Christ Superstar Various Artists
## 3 1972 Harvest Neil Young
## 4 1973 The World is a Ghetto War
## 5 1974 Goodbye Yellow Brick Road Elton John
## 6 1975 Elton John's Greatest Hits Elton John
## 7 1976 Peter Frampton Frampton Comes Alive
## 8 1977 Rumours Fleetwood Mac
## 9 1978 Saturday Night Fever Bee Gees
## 10 1979 Billy Joel 52nd Street
## # A tibble: 10 x 3
## year album band
## <chr> <chr> <chr>
## 1 1980 The Wall Pink Floyd
## 2 1981 Hi Infidelity REO Speedwagon
## 3 1982 Asia Asia
## 4 1983 Thriller Michael Jackson
## 5 1984 Thriller Michael Jackson
## 6 1985 Born in the U.S.A. Bruce Springsteen
## 7 1986 Whitney Houston Whitney Houston
## 8 1987 Slippery When Wet Bon Jovi
## 9 1988 Faith George Michael
## 10 1989 Don't Be Cruel Bobby Brown
both <- seventies %>% bind_rows(eighties)
## Warning in bind_rows_(x, .id): binding factor and character vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## # A tibble: 6 x 3
## year album band
## <chr> <chr> <chr>
## 1 1970 Bridge Over Troubled Water Simon and Garfunkel
## 2 1971 Jesus Christ Superstar Various Artists
## 3 1972 Harvest Neil Young
## 4 1973 The World is a Ghetto War
## 5 1974 Goodbye Yellow Brick Road Elton John
## 6 1975 Elton John's Greatest Hits Elton John
Results
# sixties$year is a numeric and seventies$year is a factor
str(sixties)
## Classes 'tbl_df', 'tbl' and 'data.frame': 10 obs. of 3 variables:
## $ year : num 1960 1961 1962 1963 1964 ...
## $ album: chr "The Sound of Music" "Camelot" "West Side Story" "West Side Story" ...
## $ band : chr "Original Broadway Cast" "Original Broadway Cast" "Soundtrack" "Soundtrack" ...
## Classes 'tbl_df', 'tbl' and 'data.frame': 10 obs. of 3 variables:
## $ year : Factor w/ 10 levels "1970","1971",..: 1 2 3 4 5 6 7 8 9 10
## $ album: chr "Bridge Over Troubled Water" "Jesus Christ Superstar" "Harvest" "The World is a Ghetto" ...
## $ band : chr "Simon and Garfunkel" "Various Artists" "Neil Young" "War" ...
# This gives an error about not converting factor to numeric
seventies %>% bind_rows(sixties)
## Error in bind_rows_(x, .id): Column `year` can't be converted from factor to numeric
# So lets convert it....
# But this converts the year to the integer factor value, not the year. Whoops
seventies %>%
mutate(year = as.numeric(year))
## # A tibble: 10 x 3
## year album band
## <dbl> <chr> <chr>
## 1 1 Bridge Over Troubled Water Simon and Garfunkel
## 2 2 Jesus Christ Superstar Various Artists
## 3 3 Harvest Neil Young
## 4 4 The World is a Ghetto War
## 5 5 Goodbye Yellow Brick Road Elton John
## 6 6 Elton John's Greatest Hits Elton John
## 7 7 Peter Frampton Frampton Comes Alive
## 8 8 Rumours Fleetwood Mac
## 9 9 Saturday Night Fever Bee Gees
## 10 10 Billy Joel 52nd Street
# We need to make it a character first to get the year, then convert to numeric
seventies %>% mutate(year = as.numeric(as.character(year)))
## # A tibble: 10 x 3
## year album band
## <dbl> <chr> <chr>
## 1 1970 Bridge Over Troubled Water Simon and Garfunkel
## 2 1971 Jesus Christ Superstar Various Artists
## 3 1972 Harvest Neil Young
## 4 1973 The World is a Ghetto War
## 5 1974 Goodbye Yellow Brick Road Elton John
## 6 1975 Elton John's Greatest Hits Elton John
## 7 1976 Peter Frampton Frampton Comes Alive
## 8 1977 Rumours Fleetwood Mac
## 9 1978 Saturday Night Fever Bee Gees
## 10 1979 Billy Joel 52nd Street
# Now we can join the datasets correctly
seventies %>%
# Coerce seventies$year into a useful numeric
mutate(year = as.numeric(as.character(year))) %>%
# Bind the updated version of seventies to sixties
bind_rows(sixties) %>%
arrange(year)
## # A tibble: 20 x 3
## year album band
## <dbl> <chr> <chr>
## 1 1960 The Sound of Music Original Broadway Cast
## 2 1961 Camelot Original Broadway Cast
## 3 1962 West Side Story Soundtrack
## 4 1963 West Side Story Soundtrack
## 5 1964 Hello, Dolly! Original Broadway Cast
## 6 1965 Mary Poppins Soundtrack
## 7 1966 Whipped Cream & Other Delights Herb Alpert & The Tijuana Brass
## 8 1967 More of The Monkees The Monkees
## 9 1968 Are You Experienced? The Jimi Hendrix Experience
## 10 1969 In-A-Gadda-Da-Vida Iron Butterfly
## 11 1970 Bridge Over Troubled Water Simon and Garfunkel
## 12 1971 Jesus Christ Superstar Various Artists
## 13 1972 Harvest Neil Young
## 14 1973 The World is a Ghetto War
## 15 1974 Goodbye Yellow Brick Road Elton John
## 16 1975 Elton John's Greatest Hits Elton John
## 17 1976 Peter Frampton Frampton Comes Alive
## 18 1977 Rumours Fleetwood Mac
## 19 1978 Saturday Night Fever Bee Gees
## 20 1979 Billy Joel 52nd Street