I have two dataframes.
我有两个数据帧。
Seasons Frequency
DJF 1
JJA 10
MAM 20
SON 30
And
和
Seasons Frequency
DJF 1
JJA 10
MAM 20
SON 30
How can I merge these two dfs, at the same time adding the Frequency values?
如何合并这两个dfs,同时添加频率值?
Intended output:
预期产量:
Seasons Frequency
DJF 2
JJA 20
MAM 40
SON 60
3 个解决方案
#1
1
Using package plyr
使用包plyr
require(plyr)
ddply(rbind(df1, df2),
.(Seasons),
summarize,
Frequency=sum(Frequency))
#2
0
If they're in order, with no missing labels, you can just add the second column.
如果它们是有序的,没有丢失标签,您只需添加第二列。
df1$Frequency <- df1$Frequency + df2$Frequency)
If not, you can merge on the Seasons values, then add the two together and drop the extraneous column.
如果没有,您可以合并Seasons值,然后将两者一起添加并删除无关列。
final.df <- merge(df1, df2, by='Seasons')
final.df$Frequency <- final.df$Frequency.x + final.df$Frequency.y
final.df$Frequency.x <- NULL
final.df$Frequency.y <- NULL
#3
0
Since 2 dataframes have identical structure, a one line code of base R will also do:
由于2个数据帧具有相同的结构,因此基数R的单行代码也将执行:
> df3 = data.frame(Seasons = df1$Seasons, Frequency = df1$Frequency+df2$Frequency)
> df3
Seasons Frequency
1 DJF 2
2 JJA 20
3 MAM 40
4 SON 60
#1
1
Using package plyr
使用包plyr
require(plyr)
ddply(rbind(df1, df2),
.(Seasons),
summarize,
Frequency=sum(Frequency))
#2
0
If they're in order, with no missing labels, you can just add the second column.
如果它们是有序的,没有丢失标签,您只需添加第二列。
df1$Frequency <- df1$Frequency + df2$Frequency)
If not, you can merge on the Seasons values, then add the two together and drop the extraneous column.
如果没有,您可以合并Seasons值,然后将两者一起添加并删除无关列。
final.df <- merge(df1, df2, by='Seasons')
final.df$Frequency <- final.df$Frequency.x + final.df$Frequency.y
final.df$Frequency.x <- NULL
final.df$Frequency.y <- NULL
#3
0
Since 2 dataframes have identical structure, a one line code of base R will also do:
由于2个数据帧具有相同的结构,因此基数R的单行代码也将执行:
> df3 = data.frame(Seasons = df1$Seasons, Frequency = df1$Frequency+df2$Frequency)
> df3
Seasons Frequency
1 DJF 2
2 JJA 20
3 MAM 40
4 SON 60