Merging multiple CSV files

Hello,
I am trying to merge multiple csv files into one but I am not sure where to start or which package to use. My data shows yearly growth measurements per tree pre year and set up with years as rows and individual tree IDs as columns. Also, each excel file does not go back the same number of years.
Here is a sample of my data:

Year P1_132 P1_133 P1_134 P1_135 P1_136
1950 NA NA NA NA NA
1951 NA NA NA NA NA
1952 NA NA NA NA NA
1953 NA NA NA NA NA
1954 NA NA NA NA NA
1955 NA NA NA NA NA
1956 NA NA NA NA NA
1957 NA NA NA NA NA
1958 NA NA NA NA NA
1959 NA NA NA NA NA
1960 NA NA NA NA NA
1961 NA NA NA 20.18858125 NA
1962 NA NA 27.39534453 12.7295575 NA
1963 NA NA 154.0152981 349.2215505 NA
1964 NA NA 137.1622463 464.3694413 78.0692843
1965 NA NA 244.0170016 424.9999917 323.3647319
1966 NA NA 99.66211663 241.0149459 360.264101
1967 NA NA 100.228995 356.321751 1180.648796
1968 NA NA 97.90509611 46.25681023 908.5082291
1969 NA NA 89.01405903 305.2563845 1939.031302
1970 NA NA 106.6008361 341.7962926 2069.506316
1971 NA NA 166.5640161 349.0156034 1669.814602
1972 NA NA 67.46622688 492.6286672 1903.616979
1973 NA NA 68.23273151 724.2217569 2853.476861
1974 NA NA 90.15722177 92.5274686 1506.098798
1975 NA NA 47.87847523 39.70412654 4242.715975
1976 NA NA 20.63932126 64.56596195 937.5931096
1977 NA NA 69.39515849 169.2409735 3595.471865
1978 NA NA 35.06546131 126.1101799 1245.406286
1979 NA NA 94.69658753 381.02618 4060.969599
1980 NA NA 152.1366885 291.1619746 2385.113168
1981 NA NA 65.99849364 432.4572185 2022.25993
1982 NA NA 18.59714466 56.73357248 672.0563868
1983 NA NA 125.5544321 98.52966986 2979.878569
1984 NA NA 204.3797501 160.6463802 2034.977785
1985 NA NA 754.8401713 2378.510241 1998.372616
1986 NA NA 137.70304 1325.074851 892.3572196
1987 NA NA 57.99159185 506.823341 325.5534449
1988 NA 1.877192717 395.8205148 858.9299512 3022.900267
1989 25.71494432 4.036491029 22.67190657 2561.032819 662.6735554
1990 64.40709475 3.641583408 444.1798202 350.8020952 6982.344274
1991 46.43560769 3.776395432 799.7455406 3107.614497 7338.434618
1992 94.33788048 14.36135413 642.3602561 3187.84406 5080.222849
1993 109.6183107 19.84587747 408.2431795 3755.807972 10256.68718
1994 199.0265045 122.871223 76.78605366 4893.024636 8227.597397
1995 169.8649849 279.0434726 1053.180622 4396.628194 5291.681327
1996 315.7217899 619.4887547 1729.144421 3381.897779 3949.414949
1997 485.1919048 590.6487614 959.537084 4420.050464 3055.921522
1998 676.7579593 921.6196315 1519.522346 3374.18569 1007.897395
1999 573.0913928 354.1096205 548.3898006 1136.833871 173.4685142
2000 1173.858885 183.1125879 924.3375866 210.1001065 1039.618266
2001 832.5802826 312.5136143 954.9442012 454.454245 2706.574022
2002 249.6415614 517.5462722 975.226603 2638.720242 2881.529729
2003 960.4508728 745.4603903 2660.412107 1059.291576 10463.30389
2004 628.491067 2115.619282 2796.110217 6888.534143 11147.37418
2005 343.7447492 2799.437952 355.250226 4898.403033 2409.813497
2006 158.6952407 739.6195381 2234.643773 952.0563361 4788.950966
2007 151.7253378 1293.376068 3197.742716 4947.791738 8263.247637
2008 346.5450518 1612.850943 4216.915318 5989.546218 13436.89911
2009 166.9237033 1158.439455 2527.696396 10762.0156 8774.735992
2010 226.0174852 1888.104725 315.3886742 1528.640561 1773.110825
2011 324.7120259 289.1262508 327.9027782 305.9149094 685.9600455
2012 532.8433183 123.5107727 97.13176166 125.0193027 145.7334284
2013 948.5439947 272.8739909 585.759375 162.6603579 1521.746917
2014 175.5853099 584.3151221 1139.589082 1180.709643 950.9880721
2015 343.2351106 1383.763515 3065.109651 1986.677324 7263.623932
2016 279.2040205 2829.522572 885.7632554 3480.648161 14114.68311
2017 358.1403027 946.6179754 202.8212186 6023.015917 12291.5131
2018 295.9096782 1125.269228 1199.408415 4971.743316 3768.51184
2019 179.3617713 362.7378382 183.5397753 837.1782705 3180.815315
2020 429.6867453 3387.803542 1883.815618 5390.095207 9635.875341

I think {purrr} might be the package you want to use.

Clause Wilke wrote a good blog post on how you might go about it:

1 Like

Thank you, @JackDavison. I will check out that article

I would think you need to read in each file and change the format to be one row per tree/year combination
I have never had to do that in R but I expect there is a function
Then use rbind on the resultant frames to build one merged superframe

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.