I have multiple data.frame
where each has same weather stations' coordinate but contains different year's temperature observation. However, I intend to construct new data.frame where stations' coordinate will stay but respective annual temperature column will be added programmatically from original multiple data.frame. Perhaps using dplyr
package could help, but I have some issue to concatenate Year
and Annual_Temp
column and construct new column programmatically. Because I have 35 data.frames where each has same ID
, long
, lat
, but Annual_Temp
are different from one to another. I need to construct clean tabular data by merging data.frame. How can I make this happen in R? Any way to get this done by using dplyr
? Any idea?
For example, here is head of first three data.frame:
> multiple_DF
$air_temp.1980
Year ID long lat Annual_Temp
34090 1980 6.25_51.75 6.25 51.75 10.709091
34091 1980 6.25_51.25 6.25 51.25 10.581818
34092 1980 6.25_50.75 6.25 50.75 9.500000
34224 1980 6.75_51.75 6.75 51.75 10.354545
34225 1980 6.75_51.25 6.75 51.25 10.636364
34226 1980 6.75_50.75 6.75 50.75 9.872727
$air_temp.1981
Year ID long lat Annual_Temp
119884 1981 6.25_51.75 6.25 51.75 10.727273
119885 1981 6.25_51.25 6.25 51.25 10.563636
119886 1981 6.25_50.75 6.25 50.75 9.654545
120018 1981 6.75_51.75 6.75 51.75 10.409091
120019 1981 6.75_51.25 6.75 51.25 10.654545
120020 1981 6.75_50.75 6.75 50.75 9.954545
$air_temp.1982
Year ID long lat Annual_Temp
205678 1982 6.25_51.75 6.25 51.75 11.80909
205679 1982 6.25_51.25 6.25 51.25 11.58182
205680 1982 6.25_50.75 6.25 50.75 10.61818
205812 1982 6.75_51.75 6.75 51.75 11.44545
205813 1982 6.75_51.25 6.75 51.25 11.73636
205814 1982 6.75_50.75 6.75 50.75 10.85455
Desired output (UPDATE):
I want to produce new data.frame where Annual_Temp
will be added as the new column where Annual_Temp
and Year
must be concatenated. Here is the desired data.frame that I want to have:
ID long lat Ann_temp_1980 Ann_temp_1981 Ann_temp_1982
1 6.25_51.75 6.25 51.75 10.709091 10.727273 11.80909
2 6.25_51.25 6.25 51.25 10.581818 10.563636 11.58182
3 6.25_50.75 6.25 50.75 9.500000 9.654545 10.61818
4 6.75_51.75 6.75 51.75 10.354545 10.409091 11.44545
5 6.75_51.25 6.75 51.25 10.636364 10.654545 11.73636
6 6.75_50.75 6.75 50.75 9.872727 9.954545 10.85455
How can I make this happen programmatically in R? Any idea?
To repro example data:
multiple_DF = structure(list(air_temp.1980 = structure(list(Year = c(1980L,
1980L, 1980L, 1980L, 1980L, 1980L), ID = c("6.25_51.75", "6.25_51.25",
"6.25_50.75", "6.75_51.75", "6.75_51.25", "6.75_50.75"), long = c(6.25,
6.25, 6.25, 6.75, 6.75, 6.75), lat = c(51.75, 51.25, 50.75, 51.75,
51.25, 50.75), Annual_Temp = c(10.709091, 10.581818, 9.5, 10.354545,
10.636364, 9.872727)), .Names = c("Year", "ID", "long", "lat",
"Annual_Temp"), row.names = c(NA, -6L), class = "data.frame"),
air_temp.1981 = structure(list(Year = c(1981L, 1981L, 1981L,
1981L, 1981L, 1981L), ID = c("6.25_51.75", "6.25_51.25",
"6.25_50.75", "6.75_51.75", "6.75_51.25", "6.75_50.75"),
long = c(6.25, 6.25, 6.25, 6.75, 6.75, 6.75), lat = c(51.75,
51.25, 50.75, 51.75, 51.25, 50.75), Annual_Temp = c(10.727273,
10.563636, 9.654545, 10.409091, 10.654545, 9.954545)), .Names = c("Year",
"ID", "long", "lat", "Annual_Temp"), row.names = c(NA, -6L
), class = "data.frame"), air_temp.1982 = structure(list(
Year = c(1982L, 1982L, 1982L, 1982L, 1982L, 1982L), ID = c("6.25_51.75",
"6.25_51.25", "6.25_50.75", "6.75_51.75", "6.75_51.25",
"6.75_50.75"), long = c(6.25, 6.25, 6.25, 6.75, 6.75,
6.75), lat = c(51.75, 51.25, 50.75, 51.75, 51.25, 50.75
), Annual_Temp = c(11.80909, 11.58182, 10.61818, 11.44545,
11.73636, 10.85455)), .Names = c("Year", "ID", "long",
"lat", "Annual_Temp"), row.names = c(NA, -6L), class = "data.frame")), .Names = c("air_temp.1980",
"air_temp.1981", "air_temp.1982"))