I need to append a column results that contains an average per each country from one data frame (df) to another data frame that contains countries as well (df1).If there are countries that are not appearing in the aggregated table than I should get an empty cell. Here is my code for creating the average per country of the first data frame:
df <- read.table(text = "target birds wolfs Country
3 9 7 a
3 8 4 b
1 2 8 c
1 2 3 a
1 8 3 a
6 1 2 a
6 7 1 b
6 1 5 c ",header = TRUE)
dfCountries<-summaryBy(wolfs ~ Country , data = df, FUN = mean)
dfCountries
Country wolfs.mean
1 a 3.75
2 b 2.50
3 c 6.50
Now I would like to append those results per country on a new data frame in a new column:Append_Country.How can I do it?
Here is the df1 data:
df1<-read.table(text = " target birds wolfs Country
6 4 5 a
4 5 3 a
3 8 2 a
1 6 4 b
3 5 1 a
2 2 1 b
9 9 4 b
8 9 5 f
2 3 1 f ",header = TRUE)
This is the output that I would like to get:
df1
target birds wolfs Country Append_Country
6 4 5 a 3.75
4 5 3 a 3.75
3 8 2 a 3.75
1 6 4 b 2.50
3 5 1 a 3.75
2 2 1 b 2.50
9 9 4 b 2.50
8 9 5 f
2 3 1 f