r - 按组平均丢失任何列
问题描述
我需要在不丢失任何列的情况下计算下面示例表的每组(即每个坐标)的平均值(实际表有超过 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 |
有什么建议么?
解决方案
@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
推荐阅读
- python-3.x - webp2y XML助手清理python3下的换行符
- r - 将 NetCDF 文件日期转换为 R 中的日期
- php - 如何组合多个排序功能
- javascript - 带有打字稿的 ReactJS
- javascript - 异步调用仅在 Fire Fox 上不一致
- powershell - 为什么在远程 PowerShell 会话中从 FTP 检索到的文件是加密的?
- java - java.lang.NullPointerException:尝试在空对象上调用虚拟方法 'android.view.View android.view.View.findViewById(int)'
- google-apps-script - 如何使用 Apps 脚本创建共享云端硬盘?
- python - Python - 处理后将块列表放入数据帧
- javascript - Webpack 捆绑 PNG 图像,但 React 不会显示它