首页 > 解决方案 > 按组平均丢失任何列

问题描述

我需要在不丢失任何列的情况下计算下面示例表的每组(即每个坐标)的平均值(实际表有超过 40,000 行具有不同的状态、位置坐标和类型)所以这个:

状态 位置坐标 类型 2002年 2003年 2004年 2005年 2006年 2007年 2008年 2009 2010
加利福尼亚 西方 债务 234 56 79 890 24 29 20 24 26
内华达州 西方 债务 45 54 87 769 54 76 90 87 98

会变成这样:

状态 位置坐标 类型 2002年 2003年 2004年 2005年 2006年 2007年 2008年 2009 2010
西方 西方 债务 234 56 79 890 24 29 20 24 26

当我使用聚合 (df <- 聚合(df[,4:length(df)], list(df$Coordinates), mean)。它删除了州和城市列。

位置坐标 2002年 2003年 2004年 2005年 2006年 2007年 2008年 2009 2010
西方 235 55 83 843 24 29 20 24 26 债务 54 769 76 87

当我使用 sqldf 它平均一年并变成这样:

状态 位置坐标 类型 2002年 2003年 2004年 2005年 2006年 2007年 2008年 2009 2010
西方 西方 债务 2002年 2003年 2004年 2005年 2006年 2007年 2008年 2009 2010

有什么建议么?

标签: rdataframeaggregatesqldfspread

解决方案


@akrun:这能行吗:

df %>% 
  group_by(Coordinates) %>% 
  summarise(State=Coordinates, Type=Type, across(where(is.numeric), ~mean(.x, na.rm = TRUE))) %>% 
  filter(row_number() %% 2 == 0) ## Select even rows

输出:

# Groups:   Coordinates [1]
  Coordinates State Type  `2002` `2003` `2004` `2005` `2006` `2007` `2008` `2009` `2010`
  <chr>       <chr> <chr>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 West        West  Debt    140.     55     83   830.     39   52.5     55   55.5     62

推荐阅读