在ddplyr中创建一个包含多个分组总和的新列
我在ddplyr中有一个数据帧(x),看起来像这样:在ddplyr中创建一个包含多个分组总和的新列,r,dplyr,formatting,summarize,R,Dplyr,Formatting,Summarize,我在ddplyr中有一个数据帧(x),看起来像这样: Location Type Method Observations Outside Small A 1 Outside Large A 5 Inside Small A 20 Inside Large A 17 Outside Small B 24 Outside Large B 0 Inside
Location Type Method Observations
Outside Small A 1
Outside Large A 5
Inside Small A 20
Inside Large A 17
Outside Small B 24
Outside Large B 0
Inside Small B 0
Inside Large B 12
Outside Small C 6
Outside Large C 0
Inside Small C 1
Inside Large C 85
Location Method Total_Observations
Outside A 6
Inside A 37
Outside B 24
Inside B 12
Outside C 6
Inside C 86
我要做的是创建一个新的数据框(y),它列出了按位置和方法列出的总观测值。比如说:
Location Type Method Observations
Outside Small A 1
Outside Large A 5
Inside Small A 20
Inside Large A 17
Outside Small B 24
Outside Large B 0
Inside Small B 0
Inside Large B 12
Outside Small C 6
Outside Large C 0
Inside Small C 1
Inside Large C 85
Location Method Total_Observations
Outside A 6
Inside A 37
Outside B 24
Inside B 12
Outside C 6
Inside C 86
基本上,我需要折叠类型,以便将所有这些观察值(对于每个位置和方法)添加到一起并放置在一列中
到目前为止,我得到的是:
y <- x %>%
group_by(Location,Method) %>%
replace(is.na(.), 0) %>%
summarise(Total_Observations = sum(Observations))
y%
分组依据(位置、方法)%>%
替换(is.na(.),0)%>%
总结(总观察值=总和(观察值))
问题是,这个选项(以及我尝试过的所有其他选项)给了我行数的总和,而不是将行数相加。有人知道如何解决这个问题吗?根据您想要的结果,您的脚本似乎已经达到了您想要的效果。将每个位置和方法的所有观测值相加。(例如,对于外部和A,有1个和5个观察值,您希望总的_观察值为6)Dplyr默认情况下,按第一列排序项目。为了使结果与上面的结果完全相同,我只添加了一个“arrange”语句
library(dplyr)
y <- x %>%
group_by(Location,Method) %>%
replace(is.na(.), 0) %>%
summarise(Total_Observations = sum(Observations)) %>%
arrange(Method, desc(Location))
库(dplyr)
y%
分组依据(位置、方法)%>%
替换(is.na(.),0)%>%
总结(总观察值=总和(观察值))%>%
排列(方法、描述(位置))
“此选项(以及我尝试过的所有其他选项)为我提供行数的总和,而不是将行数相加。”。您的输出表和代码告诉我不是这样。你确定这不是你要找的吗?