Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于3列(重复)合并行&;求和_R - Fatal编程技术网

R 基于3列(重复)合并行&;求和

R 基于3列(重复)合并行&;求和,r,R,我正在尝试在R中合并此数据帧中的重复项 first last city in out john doe sf 1 0 mary jane cl 0 1 john doe sf 1 0 mary jane cl 1 0 john shmo dn 1 0 为此, john doe sf 2 0 mary jane cl 1 1 john

我正在尝试在R中合并此数据帧中的重复项

first last  city    in   out   
john  doe   sf      1    0
mary  jane  cl      0    1 
john  doe   sf      1    0
mary  jane  cl      1    0 
john  shmo  dn      1    0
为此,

john  doe   sf    2    0
mary  jane  cl    1    1 
john  shmo  dn    1    0

我只是想把菲尔的回答从评论转移到实际的回答。使用
dplyr
可能是解决问题的最简单方法

mydf%>%分组依据(第一、最后、城市)%>%
汇总(输入=和(输入),输出=和(输出))%>%
解组()

只是不要忘记
解组
命令。忽略这一点可能会导致一些令人不快的惊喜。

使用
dplyr
mydf%%>%groupby(first,last,city)%%>%summary(in=sum(in),out=sum(out))%%>%ungroup()
如果数据中有城市这样的字符数据,也需要替换NA怎么办?约翰·多伊sf 1 0玛丽·简cl 0 1约翰·多伊·纳10玛丽·简·纳10约翰·什莫dn 1 0