dplyr / tidyr-汇总条件数据 [英] dplyr / tidyr - Summarise data with conditions

查看:94
本文介绍了dplyr / tidyr-汇总条件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题
我正在尝试使用dyplr& tidyr以获得输出表(例如我认为的列联表),该输出表将该数据汇总为频率(例如,负号,中性号和正号的标题,说明和正文的计数)。我尝试了许多不同的方法,最近的示例可以在使用Tidyr / Dplyr汇总字符串组的计数

示例数据
数据看起来有点像...

Example Data The data looks a little like...

df <- data.frame( "story_title"=c(0.0,0.0,0.0,-1.0,1.0),
                  "story_description"=c(-0.3,-0.3,-0.3,0.5,0.3),
                  "story_body"=c(-0.3,0.2,0.4,0.2,0))

所需的输出
希望输出看起来像这样,显示每个的摘要频率故事部分...

Desired Output The output would hopefully look a bit like this, showing the summary frequencies for each story part...

                  Negative  Neutral  Positive 
story_title              1         3        1         
story_description        3         0        2
story_body               1         1        3

(story_body的已编辑总计-感谢Akrun)

(edited totals for story_body - Thanks Akrun)

尝试的方法

如果我是对的话,第一步就是使用<$来重塑数据c $ c>聚集这样...

If I'm right the first step will be to reshape the data using gather thusly...

df <- df %>% gather(type,score,starts_with("story"))

> df 
      type score
1        story_title   0.0
2        story_title   0.0
3        story_title   0.0
4        story_title  -1.0
5        story_title   1.0
6  story_description  -0.3
7  story_description  -0.3
8  story_description  -0.3
9  story_description   0.5
10 story_description   0.3
11        story_body  -0.3
12        story_body   0.2
13        story_body   0.4
14        story_body   0.2
15        story_body   0.0

从这里开始它是group_by和summary的组合,我已经尝试过...

From here I think it's a combination of group_by and summarise and I've tried...

df %>% group_by(sentiment) %>%
          summarise(Negative = count("sentiment_title"<0),
                    Neutral  = count("sentiment_title"=0),
                    Positive  = count("sentiment_title">0)
                   )

显然,这没有用。

有人可以提供dplyr / tidyr解决方案的帮助(示例中的基表答案也很有用)吗?

推荐答案

TRy

library(dplyr)
library(tidyr)
gather(df) %>% 
      group_by(key,value= sign(value))%>%
      tally()  %>% 
      mutate(ind= factor(value, levels=c(-1,0,1), 
                    labels=c('Negative', 'Neutral', 'Positive'))) %>% 
      select(-value) %>% 
      spread(ind, n, fill=0)

这篇关于dplyr / tidyr-汇总条件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆