R 如何将分组结果从一个数据帧追加到另一个数据帧
我需要附加一列结果,其中包含每个国家从一个数据框(df)到另一个数据框(df1)的平均值。如果有国家未出现在聚合表中,我应该得到一个空单元格。 以下是我创建第一个数据帧每个国家平均值的代码:R 如何将分组结果从一个数据帧追加到另一个数据帧,r,dataframe,R,Dataframe,我需要附加一列结果,其中包含每个国家从一个数据框(df)到另一个数据框(df1)的平均值。如果有国家未出现在聚合表中,我应该得到一个空单元格。 以下是我创建第一个数据帧每个国家平均值的代码: df <- read.table(text = "target birds wolfs Country 3 9 7 a 3
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
您可以尝试将
merge()
与选项all=TRUE
一起使用:
df3 <- merge(df1,df2, by="Country", all=TRUE)
#> df3
# Country target birds wolfs Append_Country
#1 a 4 5 3 3.75
#2 a 3 8 2 3.75
#3 a 6 4 5 3.75
#4 a 3 5 1 3.75
#5 b 2 2 1 2.50
#6 b 9 9 4 2.50
#7 b 1 6 4 2.50
#8 f 2 3 1 NA
#9 f 8 9 5 NA
#10 c NA NA NA 6.50
此外,我们还可以根据所需的输出对列序列重新排序以获得结果:
df3 <- df3[, c(2,3,4,1,5)]
#> df3
# target birds wolfs Country Append_Country
#1 4 5 3 a 3.75
#2 3 8 2 a 3.75
#3 6 4 5 a 3.75
#4 3 5 1 a 3.75
#5 2 2 1 b 2.5
#6 9 9 4 b 2.5
#7 1 6 4 b 2.5
#8 2 3 1 f
#9 8 9 5 f
#10 c 6.5
df3-df3
#目标鸟沃尔夫斯郡
#1 4 5 3 a 3.75
#2 3 8 2 a 3.75
#3645A3.75
#4 3 5 1 a 3.75
#52B1B2.5
#6944B2.5
#7164B2.5
#8 2 3 1楼
#9895F
#10 c 6.5
数据:
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)
df2 <- aggregate(wolfs ~ Country , data = df, FUN = mean)
colnames(df2) <- c("Country", "Append_Country")
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)
df我正在使用sqldf包添加我自己的答案,尽管我认为@RHertel的解决方案更好,因为他的解决方案基于base-R函数
df_sq<-sqldf("select country as country_main, avg(wolfs) as Append_Country from df group by Country" )
df_sq
country_main Append_Country
1 a 3.75
2 b 2.50
3 c 6.50
df_sq1<-sqldf("select df1.*,df_sq.Append_Country from df1 left join df_sq on df_sq.country_main=df1.Country")
df_sq1
target birds wolfs Country Append_Country
1 6 4 5 a 3.75
2 4 5 3 a 3.75
3 3 8 2 a 3.75
4 1 6 4 b 2.50
5 3 5 1 a 3.75
6 2 2 1 b 2.50
7 9 9 4 b 2.50
8 8 9 5 c 6.50
9 2 3 1 c 6.50
df_squ不客气;很高兴我能提供帮助:)标记不应在for R data.frames中使用。
df3 <- df3[, c(2,3,4,1,5)]
#> df3
# target birds wolfs Country Append_Country
#1 4 5 3 a 3.75
#2 3 8 2 a 3.75
#3 6 4 5 a 3.75
#4 3 5 1 a 3.75
#5 2 2 1 b 2.5
#6 9 9 4 b 2.5
#7 1 6 4 b 2.5
#8 2 3 1 f
#9 8 9 5 f
#10 c 6.5
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)
df2 <- aggregate(wolfs ~ Country , data = df, FUN = mean)
colnames(df2) <- c("Country", "Append_Country")
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)
df_sq<-sqldf("select country as country_main, avg(wolfs) as Append_Country from df group by Country" )
df_sq
country_main Append_Country
1 a 3.75
2 b 2.50
3 c 6.50
df_sq1<-sqldf("select df1.*,df_sq.Append_Country from df1 left join df_sq on df_sq.country_main=df1.Country")
df_sq1
target birds wolfs Country Append_Country
1 6 4 5 a 3.75
2 4 5 3 a 3.75
3 3 8 2 a 3.75
4 1 6 4 b 2.50
5 3 5 1 a 3.75
6 2 2 1 b 2.50
7 9 9 4 b 2.50
8 8 9 5 c 6.50
9 2 3 1 c 6.50