【问题标题】:dplyr / tidyr - Summarise data with conditionsdplyr / tidyr - 用条件汇总数据
【发布时间】:2016-02-07 12:36:10
【问题描述】:

问题 我正在尝试使用 dyplr 和 tidyr 来实现一个输出表(就像我认为的列联表),它将这些数据总结为频率(例如,标题、描述和正文的计数是负数、中性数和正数)。我尝试了许多不同的方法,我能找到的最接近的例子是Using Tidyr/Dplyr to summarise counts of groups of strings。但这不合适。

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

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))

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

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

(story_body 的编辑总数 - 感谢 Akrun)

尝试的方法

如果我是对的,那么第一步就是使用gather 重塑数据...

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 和 summarise 的组合,我已经尝试过......

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

显然这没有奏效。

任何人都可以提供 dplyr/tidyr 解决方案(基表答案也可以作为示例)吗?

【问题讨论】:

  • 我认为story_body 应该是1 1 3

标签: r dplyr tidyr


【解决方案1】:

试一试

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)

【讨论】:

  • 我喜欢sign 的想法。我可能会用它来缩短我的时间。
  • 当然不是我想象的那么简单的任务,并且同意 sign() 是一个厚颜无耻的小举动。
  • @BarneyC 稍后我有空时会更新一些解释。
【解决方案2】:

尝试使用cut 重新标记这三个类别。然后只需使用gather 融合数据并使用dcast 重塑“宽”。

library(tidyr)
library(reshape2)
df[] <- lapply(df, function(x) {cut(x, c(-Inf,-1e-4,0,Inf), c("Negative", "Neutral", "Positive"))})
dcast(gather(df), key~value)
#            key Negative Neutral Positive
#1       story_title        1       3        1
#2 story_description        3       0        2
#3        story_body        1       1        3

【讨论】:

    【解决方案3】:

    为什么不直接使用原生 R 的 xtabs?

    从您的代码开始:

    >df <- df %>% gather(type,score,starts_with("story"))
    >df$movement<-ifelse(df$score ==0 ,"Neutral",ifelse(df$score < 0 ,"Negative","Positive"))
    >xtabs(~df$type+df$movement)
    
                          df$movement
      df$type             Negative Neutral Positive
      story_title              1       3        1
      story_description        3       0        2
      story_body               1       1        3
    

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 2015-10-05
      • 2019-03-06
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2020-06-25
      相关资源
      最近更新 更多