Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go

Combine Rows

20201026 We can utilise dplyr::bind_rows() to combine the rows of two datasets with the same columns:

bind_rows(head(ds), tail(ds))
## # A tibble: 12 x 24
##    date       location min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>       <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2008-12-01 Albury       13.4     22.9      0.6          NA       NA
##  2 2008-12-02 Albury        7.4     25.1      0            NA       NA
##  3 2008-12-03 Albury       12.9     25.7      0            NA       NA
##  4 2008-12-04 Albury        9.2     28        0            NA       NA
##  5 2008-12-05 Albury       17.5     32.3      1            NA       NA
##  6 2008-12-06 Albury       14.6     29.7      0.2          NA       NA
##  7 2020-04-24 Uluru        23       36.7      0            NA       NA
##  8 2020-04-25 Uluru        18.4     37.4      0            NA       NA
##  9 2020-04-26 Uluru        21.4     32.7      0            NA       NA
## 10 2020-04-27 Uluru        19.4     32.2      0            NA       NA
## 11 2020-04-28 Uluru        16.6     32.6      0            NA       NA
## 12 2020-04-29 Uluru        16.7     25.7      0            NA       NA
## # ... with 17 more variables: wind_gust_dir <ord>, wind_gust_speed <dbl>,
## #   wind_dir_9am <ord>, wind_dir_3pm <ord>, wind_speed_9am <dbl>,
## #   wind_speed_3pm <dbl>, humidity_9am <int>, humidity_3pm <int>,
## #   pressure_9am <dbl>, pressure_3pm <dbl>, cloud_9am <int>, cloud_3pm <...
## #   temp_9am <dbl>, temp_3pm <dbl>, rain_today <fct>, risk_mm <dbl>,
## #   rain_tomorrow <fct>

The operation performed by dplyr::bind_rows() is also useful to convert a named list (or vector if all values are of the same type) into a tibble:

ds %>%
  head() %>%
  select(2:5) %>%
  bind_rows(list(location="Somewhere", min_temp=-10,
                 max_temp=30, rainfall=10))
## # A tibble: 7 x 4
##   location  min_temp max_temp rainfall
##   <chr>        <dbl>    <dbl>    <dbl>
## 1 Albury        13.4     22.9      0.6
## 2 Albury         7.4     25.1      0  
## 3 Albury        12.9     25.7      0  
## 4 Albury         9.2     28        0  
## 5 Albury        17.5     32.3      1  
## 6 Albury        14.6     29.7      0.2
## 7 Somewhere    -10       30       10


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.