Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine Data.Frames with different number of columns

I have two data.frames, a and b, in which a‘s columns are a subset of b‘s columns. I want to concatenate a and b and fill with NA where necessary. Is there a function that does this easily?

Here are the data.frames:

> a
                   1979     1980     1981     1982     1983     1984     1985
area_harvested 51.22632 51.22632 41.62139 57.62961 54.42797 54.42797 38.41974
                   1986     1987     1988     1989     1990     1991     1992
area_harvested 61.47159 40.02057 48.02468 38.41974 44.82303 24.01234 22.41152
                   1993     1994     1995     1996     1997     1998     1999
area_harvested 22.41152 16.00823 29.13497 24.01234 25.61316 32.01645 32.01645
                   2000   2001     2002     2003     2004    2005     2006
area_harvested 26.89382 25.293 20.49053 18.24938 18.56954 19.8502 16.64856
                   2007     2008     2009     2010     2011     2012     2013
area_harvested 16.64856 22.73168 16.64856 17.92921 12.48642 10.24526 11.20576
                   2014     2015     2016     2017     2018     2019     2020
area_harvested 11.20576 14.08724 12.80658 12.80658 13.12675 13.76707 12.48642
                   2021
area_harvested 15.04773

> b
             1980    1981     1982     1983     1984     1985     1986     1987
import_p 20.46107 18.7567 16.89872 15.22738 10.74414 11.56598 11.94875 14.14608
             1988     1989     1990     1991     1992     1993    1994     1995
import_p 13.26701 11.94164 10.73588 9.207249 7.090021 8.310488 10.4217 13.34546
             1996     1997     1998     1999     2000     2001     2002    2003
import_p 10.88678 9.163747 8.057007 5.915273 4.326179 4.334085 4.655614 5.28608
             2004     2005     2006     2007     2008     2009     2010
import_p 6.420974 6.571366 6.735536 11.21129 13.33486 11.58492 10.44943
             2011     2012     2013     2014     2015     2016     2017
import_p 11.78057 10.91844 8.063265 8.646034 9.146645 9.594348 9.817362
             2018     2019     2020     2021    2022     2023     2024     2025
import_p 9.047568 10.69488 11.05412 8.536996 10.1282 9.454707 9.531522 9.247525
             2026     2027     2028     2029     2030     2031     2032
import_p 8.999797 8.739282 8.473118 8.205516 7.937321 7.712599 7.435103
             2033     2034     2035     2036    2037     2038     2039     2040
import_p 7.175643 6.912851 6.725843 6.602985 6.44261 6.269521 5.937039 5.869732
             2041     2042     2043     2044    2045     2046     2047     2048
import_p 5.850492 5.648758 5.427907 5.185563 4.94964 4.714463 4.490712 4.260437
             2049     2050     2051     2052 2053
import_p 4.024635 3.827092 3.143441 3.283348   NA


MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use bind_rows from dplyr

dplyr::bind_rows(a,b)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading