【问题标题】:Subsetting output in R to generate summariesR中的子集输出以生成摘要
【发布时间】:2017-10-11 12:49:48
【问题描述】:

以下是我进行的一组营销活动的数据框 (DF)。成功百分比显示了受访者的数量,沟通类型是指渠道-facebook、twitter 等(Ai-FB、L1-Linkedin)。 Laptop、New 和 mouse 指的是组成句子的单词。原始数据框有一句话——比如购买一台新笔记本电脑并免费获得鼠标。我已经用 qdap 解析了上面的内容,并在下面生成了 DF。

 Sl NO  Success_Percentage  communication_type  Laptop  New    Mouse
   1    35.46666667             email              1      0      0
   2    32.32830821             email              1      0      1
   3    22.9226361              SMS                0      1      0

   4    21.88888889             SMS                1      1      0
   5    40.04085802             FB                 0      1      1
   6    38.7283237              FB                 1      0      1

我根据通讯类型整理了DF。组成句子的三个单词下的值表示在通信过程中发送的原始句子中是否存在相同的值。

所有通信中最常见的关键字由以下代码给出

   Wordlist2<-as.data.frame(colSums(DF)[colSums(DF)>0])

Wordlist2 的输出如下

    Laptop  4
    New     3
    Mouse   3

我想知道如何获得上述按通信类型子集的输出。它可以手动完成,但我想知道是否存在任何可以做到这一点的包。

 require(dplyr)
  DF%>%
     group_by(communication_type, Success_Percentage)%>%
     summarise(colSums(DF))

但这不起作用。

【问题讨论】:

  • 您确定要按Success_Percentage 分组吗?这是一个连续值,在您的示例中是独一无二的

标签: r dplyr subset


【解决方案1】:

我不使用dplyr,但我有data.table的解决方案:

#---Input data
DF <- read.table(text = "Sl_NO  Success_Percentage  communication_type  Laptop  New    Mouse
                       1    35.46666667             email              1      0      0
                       2    32.32830821             email              1      0      1
                       3    22.9226361              SMS                0      1      0
                       4    21.88888889             SMS                1      1      0
                       5    40.04085802             FB                 0      1      1
                       6    38.7283237              FB                 1      0      1", header = T)

DF <- as.data.table(DF) #---Convert DF from data.frame to data.table

DF[, .N, by = communication_type] 

生产:

   communication_type N
1:              email 2
2:                SMS 2
3:                 FB 2

编辑:

DF[, .(Laptop = sum(Laptop), New = sum(New), Mouse = sum(Mouse)), by = communication_type]

生产:

   communication_type Laptop New Mouse
1:              email      2   0     1
2:                SMS      1   2     0
3:                 FB      1   1     2

【讨论】:

  • 整洁。很简单。
【解决方案2】:

我不完全确定这是你想要的,但无论如何我很确定你想要summarise_at

DF <- read.table(text="Sl_NO  Success_Percentage  communication_type  Laptop  New    Mouse
   1    35.46666667             email              1      0      0
   2    32.32830821             email              1      0      1
   3    22.9226361              SMS                0      1      0
   4    21.88888889             SMS                1      1      0
   5    40.04085802             FB                 0      1      1
   6    38.7283237              FB                 1      0      1",stringsAsFactors=F,header=T)

require(dplyr)
DF %>% group_by(communication_type) %>% summarise_at(c("Laptop","New","Mouse"),sum)

# # A tibble: 3 x 4
#   communication_type Laptop   New Mouse
#                <chr>  <int> <int> <int>
# 1              email      2     0     1
# 2                 FB      1     1     2
# 3                SMS      1     2     0

【讨论】:

  • 谢谢。我需要对列进行子集化,但效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 2021-03-16
  • 2021-11-09
  • 2018-10-26
  • 1970-01-01
  • 2020-08-22
相关资源
最近更新 更多