按两个变量分组汇总

编程入门 行业动态 更新时间:2024-10-28 18:22:08
本文介绍了按两个变量分组汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑一个简化的数据集(实际数据集具有更多的行和列):

Consider a simplified dataset (the real one has more columns and rows):

df tp tf weight 1 FWD RF 78.86166 2 MF LF 81.04566 3 DEF LF 80.70527 4 DEF LF 82.96071 5 DEF RF 78.42544 6 GK LF 79.37686 7 DEF RF 78.79928 8 MF RF NA 9 MF RF 78.93815 10 DEF RF 80.00284

我想通过分组的tp和tf的中位数来填充重量的缺失值

I want to fill the missing values in weight by the grouped median of tp and tf combined

什么我直到现在都尝试过以下操作(我使用过dlpyr)

What i have tried till now is the following (I have used dlpyr)

temp <- df %>% group_by(tp,tf) %>% summarise(mvalue = median(weight,na. rm = TRUE))

这使temp为:

temp Source: local data frame [6 x 3] Groups: tp [?] tp tf mvalue <fctr> <fctr> <dbl> 1 DEF LF 81.83299 2 DEF RF 78.79928 3 FWD RF 78.86166 4 GK LF 79.37686 5 MF LF 81.04566 6 MF RF 78.93815

现在,我无法弄清楚如何用相应的组中位数来填充df中的缺失值。

Now i am unable to figure out how to fill the missing values in df with the corresponding group median.

在我的简单情况下,只有一个NA对应于tp = MF和tf = RF,如果您看一下温度,的中位数为78.93815

In my simple case there is only one NA corresponding to tp = MF and tf = RF, the median value if you look up at temp is 78.93815

一般而言,我该怎么做?不要建议您是否有比我最初的方法更好的方法。

How do i do this in general? Do suggest if you have a better approach than my initial one.

编辑: 如果

推荐答案

您可以尝试,

library(dplyr) df %>% group_by(tp, tf) %>% mutate(weight = replace(weight, is.na(weight), median(weight, na.rm = TRUE)))

更多推荐

按两个变量分组汇总

本文发布于:2023-11-22 00:36:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!