I found my self in a bit of a quandry. I had a data frame which contained many columns that needed to be squished together – each contained a few values, but mostly NAs, and none of the values were overlapping. They represented the same variable, but broken apart across several columns. I needed to condense several columns into one.
Having extracted climate data from a whole mess of geoTIFFs, I was left with a data table that looked something like this:
> head(clim) alt_07.tif alt_11.tif alt_12.tif alt_15.tif alt_16.tif alt_17.tif bio1_07.tif bio1_11.tif bio1_12.tif bio1_15.tif CA001 NA NA NA NA 61 NA NA NA NA NA CA008 NA 24 NA NA NA NA NA 100 NA NA CA009 NA NA 1308 NA NA NA NA NA 39 NA CA010 NA NA 599 NA NA NA NA NA 73 NA US014 NA NA 935 NA NA NA NA NA 57 NA BG001 NA NA NA NA 1 NA NA NA NA NA
Each row is a location, and each column is a file that I pulled data from. All the columns that have the same characters before the “_” represent the same variable, here “alt” is altitude and “bio1” is annual mean temperature. The number after the “_” indicates which part of the globe or which tile it came from.
So, with the help of Andrew MacDonald, @polesasunder, I came up with some functions to squish the columns representing the same climate variable together. Phew! I will need to do this again for sure, but perhaps it would also be useful in other situations?