使用 dplyr 将总计数字列 wrt 汇总到分类列
summarise totals numeric cols wrt to categorical col using dplyr
我正在尝试总结以下数据集....
这是 dput
structure(list(sentiment_desc = c("positive", "negative", "negative",
"negative", "negative", "negative", "negative", "positive", "positive",
"negative", "positive", "positive", "negative", "negative", "negative",
"positive", "positive", "positive", "positive", "positive"),
relationship = c(1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), portfolio = c(0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 0L), vfm = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ease = c(0L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L,
0L, 1L, 0L, 0L), Innovation = c(1, 0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1), Customer_Focus = c(0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), people = c(1,
1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
在最后一行。我想显示总数,即从 relationship
列到 people
的每一列的 sum
。只是添加如果有一个......然后计算每个 col
中有多少 negative
、positve
。例如:relation col
总数是 18
,10
是 positive
,8
是 negatives
我怎样才能做到这一点?
我知道有一个看门人,这是我试过的:
adorn_totals(df, where = "col", na.rm = TRUE,
name = "Total")
但这显示了奇怪的输出...
谢谢
您可以按 sentiment_desc
分组,然后通过计算总和来汇总所有列:
library(dplyr)
Sum <- df %>% group_by(sentiment_desc) %>% summarise_if(is.numeric, ~sum(.)) %>%
mutate(sentiment_desc = paste(sentiment_desc,"Total"))
# A tibble: 2 x 8
sentiment_desc relationship portfolio vfm ease Innovation Customer_Focus people
<chr> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 negative Total 8 5 1 4 1 5 7
2 positive Total 10 4 1 2 6 7 8
然后使用 bind_rows
在数据帧末尾添加两行:
library(dplyr)
DF <- df %>% bind_rows(Sum)
tail(DF)
# A tibble: 6 x 8
sentiment_desc relationship portfolio vfm ease Innovation Customer_Focus people
<chr> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 positive 1 0 0 0 1 0 1
2 positive 1 0 0 1 0 1 1
3 positive 1 1 0 0 0 1 0
4 positive 1 0 0 0 1 1 1
5 negative Total 8 5 1 4 1 5 7
6 positive Total 10 4 1 2 6 7 8
它看起来像你想要做什么吗?
我正在尝试总结以下数据集....
这是 dput
structure(list(sentiment_desc = c("positive", "negative", "negative",
"negative", "negative", "negative", "negative", "positive", "positive",
"negative", "positive", "positive", "negative", "negative", "negative",
"positive", "positive", "positive", "positive", "positive"),
relationship = c(1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), portfolio = c(0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 0L), vfm = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ease = c(0L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L,
0L, 1L, 0L, 0L), Innovation = c(1, 0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1), Customer_Focus = c(0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), people = c(1,
1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
在最后一行。我想显示总数,即从 relationship
列到 people
的每一列的 sum
。只是添加如果有一个......然后计算每个 col
中有多少 negative
、positve
。例如:relation col
总数是 18
,10
是 positive
,8
是 negatives
我怎样才能做到这一点? 我知道有一个看门人,这是我试过的:
adorn_totals(df, where = "col", na.rm = TRUE,
name = "Total")
但这显示了奇怪的输出...
谢谢
您可以按 sentiment_desc
分组,然后通过计算总和来汇总所有列:
library(dplyr)
Sum <- df %>% group_by(sentiment_desc) %>% summarise_if(is.numeric, ~sum(.)) %>%
mutate(sentiment_desc = paste(sentiment_desc,"Total"))
# A tibble: 2 x 8
sentiment_desc relationship portfolio vfm ease Innovation Customer_Focus people
<chr> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 negative Total 8 5 1 4 1 5 7
2 positive Total 10 4 1 2 6 7 8
然后使用 bind_rows
在数据帧末尾添加两行:
library(dplyr)
DF <- df %>% bind_rows(Sum)
tail(DF)
# A tibble: 6 x 8
sentiment_desc relationship portfolio vfm ease Innovation Customer_Focus people
<chr> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 positive 1 0 0 0 1 0 1
2 positive 1 0 0 1 0 1 1
3 positive 1 1 0 0 0 1 0
4 positive 1 0 0 0 1 1 1
5 negative Total 8 5 1 4 1 5 7
6 positive Total 10 4 1 2 6 7 8
它看起来像你想要做什么吗?