简体   繁体   English

汇总变量并将结果添加到现有数据框中

[英]Summarise variables and add the result to the existing data frame

I need to group by "id_group" and find the median price, and then add this in the existing df. 我需要按“ id_group”分组并找到中位数价格,然后将其添加到现有df中。

summarise will help us finding the median price after group_by but how do I add this new variable to the existing df? summarise将帮助我们找到group_by之后的中位数价格,但是如何将这个新变量添加到现有df中? mutate might also not help as it will not summarise but keep repeating the entries of the grouping variable. mutate可能也无济于事,因为它不会summarise但会不断重复分组变量的条目。

I tried ave() too but didn't help. 我也尝试过ave()但没有帮助。 Any other solution? 还有其他解决方案吗?

Existing data frame 现有数据框

1. id  group  value1 value2
    1.   a      10       1.2 
    2.   a      20       1.3
    3.   b      100      5.1 
    4.   b      200      5.4

Output needed: 需要的输出:

 1. id  group  value  grp.median.values1 grp.median.values2 
    1.   a      10     10                  1.2
    2.   b      100    100                 5.1

Mutating median after original values does not make sense. 在原始值之后突变中位数没有任何意义。 The idea of summarise() is to collapse many values to their representatives. summarise()的想法是将许多值分解为它们的代表。

Your final output example just prints one value for each group (first value1 and first value2 in a / first value1 and first value2 in b ). 您的最终输出示例仅为每个组打印一个值(a中的first value1和first value2 / ba first value1和first value2 )。
I think this means nothing. 我认为这没有任何意义。

Collapse many values down to a single summary ( summarise() ). 将许多值折叠为一个摘要( summarise() )。

[ http://r4ds.had.co.nz/transform.html#introduction-2][1] [ http://r4ds.had.co.nz/transform.html#introduction-2][1]

I think you have already know how: 我认为您已经知道如何:

library(tidyverse)

df <-
  tribble(
    ~id, ~group, ~value1, ~value2,
    "1.", "a", 10, 1.2,
    "2.", "a", 20, 1.3,
    "3.", "b", 100, 5.1,
    "4.", "b", 200, 5.4,
  )

df %>%
  group_by(group) %>%
  summarise_if(is.numeric, median)

## # A tibble: 2 x 3
##   group value1 value2
##   <chr>  <dbl>  <dbl>
## 1 a         15   1.25
## 2 b        150   5.25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM