R:两组的平均(和标准差)出现次数

时间:2019-08-07 20:29:33

标签: r dplyr

我想计算在不同CareType中接触表面的平均次数(和sd)。

head(movsdf.rbind)
# A tibble: 6 x 7
  ActivityID CareType HCWType Orientation Surface    Date       Time    
  <fct>      <fct>    <fct>   <fct>       <chr>      <date>     <time>  
1 01         IV       RN01    leftFacing  AlcOutside 0003-08-20 11:08:01
2 01         IV       RN01    leftFacing  In         0003-08-20 11:08:12
3 01         IV       RN01    leftFacing  Door       0003-08-20 11:08:12
4 01         IV       RN01    leftFacing  Door       0003-08-20 11:08:18
5 01         IV       RN01    leftFacing  Other      0003-08-20 11:08:22
6 01         IV       RN01    leftFacing  Table      0003-08-20 11:10:26

一些示例数据

movs.rbind<-data.frame(CareType=rep(c("IV","Rounds"),each=50),Surface=rep(c("Table","Chair"), each=50),ActivityID=rep(1:5,each=20))


#This gives me the number of each type of surface touched in each activityID grouped by care type but I'd like to know the mean number of times any surfaces are touched per care type
t<-aggregate(data=movsdf.rbind,Surface~CareType+ActivityID,function(x) NROW(x))

查找接触的平均表面数  我这样做:

aggregate(data=t,Surface~CareType,mean) 

dplyr中肯定有一个优雅的汇总功能吗?

1 个答案:

答案 0 :(得分:1)

有一个相当简单的dplyr解决方案:

按CareType和ActivityID进行第一个分组(顺序很重要,因为每个summarise调用都会剥离最后一个分组)。

然后使用summarise通过CareType和ActivityID计算触摸次数,然后再次汇总以仅通过CareType获得摘要统计信息。

library(dplyr)

df1<-data.frame(CareType=rep(c("IV","Rounds"),each=50),Surface=rep(c("Table","Chair"), each=50),ActivityID=rep(1:5,each=20))


df1 %>% group_by(CareType, ActivityID) %>% 
  summarise(touches = n()) %>% 
  summarise(mean_touches = mean(touches),
            sd_touches = sd(touches))

## A tibble: 2 x 3
#  CareType mean_touches sd_touches
#  <fct>           <dbl>      <dbl>
#1 IV               16.7       5.77
#2 Rounds           16.7       5.77