创建dplyr汇总表

时间:2017-02-06 13:48:49

标签: r dplyr

我有一个我想要总结的数据集。我的数据看起来像looks like this

Sheet1中的表引用原始表。 Sheet2中的表是我想要的结果,使用dplyr。

基本上,对于每个变量(我们的网站,员工的友善和食品质量),我想要一个'满意'和'非常热化'的总和,表示为参数的受访者总数的百分比。例如,互联网专栏的80%是4(满意+ V.Satisfied)/ 5(预订的互联网受访者总数)* 100 = 80%。

我使用了这段代码,但是我没有得到理想的结果:

test %>%
     group_by(Parameter.1..Mode.of.reservation,Our.Website) %>% 
     select(Our.Website,Friendliness.of.Staff,Food.Quality) %>% 
     summarise_each(funs(freq = n()))  

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

如果gather summarizing之前的数据{@ 1}},可以简化@ ira的解决方案。这样就可以跳过多个作业。

library(tidyverse)
library(googlesheets)
library(scales)

# Authorize with google.
gs_auth()

# Register the sheet
gs_data <- gs_url("https://docs.google.com/spreadsheets/d/1zljXN7oxUvij2mXHiyuRVG3xp5063chEFW_QERgHegg/")

# Read in the first worksheet
data <- gs_read(gs_data, ws = 1) 

# Summarize using tidyr/dplyr
data %>%
  gather(item, response, -1:-2) %>% 
  filter(!is.na(response)) %>% 
  group_by(`Parameter 1: Mode of reservation`, item) %>% 
  summarise(percentage = percent(sum(response %in% c("Satisfied","Very Satisfied"))/n())) %>% 
  spread(`Parameter 1: Mode of reservation`, percentage)

答案 1 :(得分:-1)

使用dplyr汇总数据后,您可以使用tidyr转置数据集,以便您按照问题中的要求获得列和行。

# read in the data
data <- read.csv("C:/RSnips/My Dataset - Sheet1.csv")
# load libraries
library(dplyr)
library(tidyr)

# take the loaded data
data2 <- data %>%
        # group it by mode of reservation
        group_by(Parameter.1..Mode.of.reservation) %>%
        # summarise
        summarise(
                # count how many times website column takes values sat or very sat and divide by number of observations in each group given by group_by
                OurWeb = sum(Our.Website == "Satisfied" |
                                      Our.Website == "Very Satisfied")/n(),
                # do the same for Staff and food
                Staff = sum(Friendliness.of.Staff == "Satisfied" |
                                      Friendliness.of.Staff == "Very Satisfied")/n(),
                Food = sum(Food.Quality == "Satisfied" |
                                     Food.Quality == "Very Satisfied")/n()) %>%
        # If you want to have email, internet and phone in columns
        # use tidyr package to transpose the dataset
        # first turn it into a long format, where mode of the original columns are your key
        gather(categories, val, 2:(ncol(data)-1)) %>%
        # then turn it back to wide format, but mode of reservation will be in columns
        spread(Parameter.1..Mode.of.reservation, val)

答案 2 :(得分:-1)

怎么样:

<?php
echo $form->field($model, 'permissions[clients][view_all]')
    ->checkbox([
        'uncheck'=>false,
        'labelOptions' => [
            'style' => 'padding-left:20px;'
        ]
    ])->label('View all');
?>