Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 数据框中未加入的单独观测值_R_Dataframe_Tidyverse_Purrr - Fatal编程技术网

R 数据框中未加入的单独观测值

R 数据框中未加入的单独观测值,r,dataframe,tidyverse,purrr,R,Dataframe,Tidyverse,Purrr,所以这有很多单独的观察结果,我需要加在一起 使用dget(): 我尝试过很多不同的解决方案,如: df %>% group_by(product, price) %>% summarise( quantity = sum(quantity), total = sum(total) ) 以及: 以及: 但我只是得到: > df quantity total 1 61 1685 预期输出如下所示,但显然product列上的所有产

所以这有很多单独的观察结果,我需要加在一起

使用dget():

我尝试过很多不同的解决方案,如:

df %>% 
  group_by(product, price) %>% 
  summarise(
    quantity = sum(quantity),
    total = sum(total)
  )
以及:

以及:

但我只是得到:

> df
  quantity total
1       61  1685
预期输出如下所示,但显然
product
列上的所有产品:

#> # Groups:   product [2]
#>   product        price quantity total
#>   <chr>          <dbl>    <dbl> <dbl>
#> 1 small cucumber    10        1    10
#> 2 tomatoes 1kg      16        2    32

#>#组:产品[2]
#>产品价格数量合计
#>                  
#>1个小黄瓜10110
#>2个西红柿1公斤16 2 32
我以前问过这个问题,但我还不够具体


谢谢。

你打的是
plyr::summary
而不是
dplyr::summary
。您可以使用

df %>%
  group_by(Product) %>%
  dplyr::summarise(
    Quantity = sum(Quantity),
    AveragePrice = sum(Total)/sum(Quantity),
    Total = sum(Total))
#> # A tibble: 27 x 4
#>    Product                      Quantity AveragePrice Total
#>    <chr>                           <dbl>        <dbl> <dbl>
#>  1 asparagus 200g                      4           45   180
#>  2 back bacon 200g                     1           30    30
#>  3 beef fillet strips 500g             1           90    90
#>  4 beetroot 1kg                        1           15    15
#>  5 broccoli head                       1           25    25
#>  6 butter 500g                         1           57    57
#>  7 butternut cubes                     4           14    56
#>  8 calistos jalape=c3=b1o salsa        1           40    40
#>  9 carrot 1 kg                         4           14    56
#> 10 cauliflower whole head              2           25    50
#> # … with 17 more rows
df%>%
组别(产品)%>%
dplyr::总结(
数量=总和(数量),
平均价格=总额/总额(数量),
总计=总和(总计))
#>#A tibble:27 x 4
#>产品数量平均价格合计
#>                                        
#>1个芦笋200g 4 45 180
#>2份背培根200g 130 30
#>3条牛柳条500g 190
#>4甜菜根1kg 11515
#>5花椰菜头12525
#>6黄油500g 157
#>7个奶油坚果方块4 14 56
#>8杯墨西哥辣椒=c3=b1o莎莎酱140
#>9胡萝卜1公斤41456
#>10花椰菜整头2 25 50
#>#…还有17行

你说,我真不敢相信事情会这么简单。非常感谢您,我已经了解了指定要使用的软件包的重要性。
#> # Groups:   product [2]
#>   product        price quantity total
#>   <chr>          <dbl>    <dbl> <dbl>
#> 1 small cucumber    10        1    10
#> 2 tomatoes 1kg      16        2    32

df %>%
  group_by(Product) %>%
  dplyr::summarise(
    Quantity = sum(Quantity),
    AveragePrice = sum(Total)/sum(Quantity),
    Total = sum(Total))
#> # A tibble: 27 x 4
#>    Product                      Quantity AveragePrice Total
#>    <chr>                           <dbl>        <dbl> <dbl>
#>  1 asparagus 200g                      4           45   180
#>  2 back bacon 200g                     1           30    30
#>  3 beef fillet strips 500g             1           90    90
#>  4 beetroot 1kg                        1           15    15
#>  5 broccoli head                       1           25    25
#>  6 butter 500g                         1           57    57
#>  7 butternut cubes                     4           14    56
#>  8 calistos jalape=c3=b1o salsa        1           40    40
#>  9 carrot 1 kg                         4           14    56
#> 10 cauliflower whole head              2           25    50
#> # … with 17 more rows