I have two data frames in R:
df1
Site_code Species_code Abundance
1 MRN 50
1 TFP 100
2 MRN 5
2 XNP 20
2 AMP 15
and in df2 I have a bunch of information, as well as the site code, but there is only one row for each site. The other columns are not of interest.
Site_code latitude mean_temp ...etc
1 55 15
2 56 10
I want to create a new dataframe (or matrix) with one row for each site in df2, based on the information in df1, with one column for each species, and with the column name the same as the species code, and the information on the abundance of each species in the column, with a 0 value if the species was not recorded at the site, so that I would get a df like:
Site_code AMP MRN TFP XNP
1 0 50 100 0
2 15 5 0 20
I also want to do this several times, since I have many df2's, and I want to make up a new dataframe for each of them.
I've read through many questions on SO and have not come across one that answers this. If it has already been answered, I would really appreciate being pointed in the right direction.