将行合为一个值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将行合为一个值相关的知识,希望对你有一定的参考价值。

我有一个数据集:

LOC     Store   Question        Rating

NYC     MCD     Food quality?       3
NYC     MCD     Water quality?      4
NYC     MCD     Cleanliness?        5

LA      BK      Food quality?       3
LA      BK      Water quality?      2   
LA      BK      Cleanliness?        5

SF      MCD     Food quality?       4
SF      MCD     Water quality?      5   
SF      MCD     Cleanliness?        4

而且我想将问题分解成各家商店的平均评分:

Loc Store   Avg.Rating
NYC MCD     (3+4+5)/3
LA  BK      (3+2+5)/3
SF  MCD     (4+5+4)/3

[tribble为方便起见:

df <- 
tribble(
~LOC,   ~Store, ~Question,  ~Rating,
"NYC",  "MCD",  "Food?",        3,
"NYC",  "MCD",  "Water?",       4,
"NYC",  "MCD",  "Clean?",       5,
"LA",   "BK",   "Food?",        3,
"LA",   "BK",   "Water?",       2,  
"LA",   "BK",   "Clean?",       5,
"SF",   "MCD",  "Food?",        4,
"SF",   "MCD",  "Water?",       5,  
"SF",   "MCD",  "Clean?",       4
)
答案

最简单的方法是使用dplyr中的'group_by'和'summarize'。

library("tibble")
library("dplyr")
library("magrittr")

df <- 
tribble(
~LOC,   ~Store, ~Question,  ~Rating,
"NYC",  "MCD",  "Food?",        3,
"NYC",  "MCD",  "Water?",       4,
"NYC",  "MCD",  "Clean?",       5,
"LA",   "BK",   "Food?",        3,
"LA",   "BK",   "Water?",       2,  
"LA",   "BK",   "Clean?",       5,
"SF",   "MCD",  "Food?",        4,
"SF",   "MCD",  "Water?",       5,  
"SF",   "MCD",  "Clean?",       4
)

store.avg <- group_by(df, LOC, Store) %>%
  summarize(Avg.Rating = mean(Rating))

# A tibble: 3 x 3
# Groups:   LOC [3]
  LOC   Store Avg.Rating
  <chr> <chr>      <dbl>
1 LA    BK          3.33
2 NYC   MCD         4   
3 SF    MCD         4.33

以上是关于将行合为一个值的主要内容,如果未能解决你的问题,请参考以下文章

关于CSS的个人理解

使用 Python pandas 根据条件将行值复制到另一列

将行与标题进行比较,然后在列中插入值并在 VBA 中进行重复检查

如何将行附加到 R 数据框

当我尝试通过 POST 将行的 id 值发送到另一个页面时,此页面将始终 GET id value 1

如何将行追加到另一个数据帧