2015-02-09 2 views
0

У меня есть таблица, которая выглядит следующим образом:подсчет частоты состояний в несколько столбцов в dataframe в R

City Browser Device Channel  Category 
A  IE  mobile International Fashion 
A  safari mobile Link   Furniture 
B  chrome desktop Link   Fashion 
B  opera  tablet Direct   Gift 
C  IE  desktop Direct   Health 
C  Kindle console Email   Health 
C  Chrome console Email   Fashion 

Что мне нужно, чтобы получить частоту каждого из суб -headings против основного заголовка который Category:

Category Sub Category Variable  Value 
Health Device  mobile   25% 
Health Device  console   40% 
Health Device  desktop   25% 
Health Device  tablet   10% 
Health Channel  International 80% 
Health Channel  Direct   20% 
Fashion 
Gift 

так далее для других категорий. Отправьте это, я хочу создать сегментированный штрих-код на основе Category и каждой подкатегории. Incase Я не получаю частоту, счет отлично, так как я могу преобразовать в частоту в ggplot.

ответ

2

(я добавил несколько строк в примере файла данных)

это то, что вы собираетесь ?:

dat <- read.table(text="City Browser Device Channel  Category 
A  IE  mobile International Fashion 
A  safari mobile Direct   Furniture 
A  safari console Email   Health 
B  chrome desktop Link   Fashion 
B  opera  tablet Direct   Gift 
B  opera  mobile Link   Furniture 
C  opera  console Direct   Gift 
C  IE  desktop Direct   Health 
C  Kindle console Email   Health 
C  Chrome console Email   Fashion", 
        header=TRUE, stringsAs=FALSE) 

library(dplyr) 
library(tidyr) 

dat %>% 
    gather(`Sub Category`, `Variable`, Device, Channel) %>% 
    count(`Category`, `Sub Category`, `Variable`) 

## Source: local data frame [16 x 4] 
## Groups: Category, Sub Category 
## 
##  Category Sub Category  Variable n 
## 1 Fashion  Device  console 1 
## 2 Fashion  Device  desktop 1 
## 3 Fashion  Device  mobile 1 
## 4 Fashion  Channel   Email 1 
## 5 Fashion  Channel International 1 
## 6 Fashion  Channel   Link 1 
## 7 Furniture  Device  mobile 2 
## 8 Furniture  Channel  Direct 1 
## 9 Furniture  Channel   Link 1 
## 10  Gift  Device  console 1 
## 11  Gift  Device  tablet 1 
## 12  Gift  Channel  Direct 2 
## 13 Health  Device  console 2 
## 14 Health  Device  desktop 1 
## 15 Health  Channel  Direct 1 
## 16 Health  Channel   Email 2 
Смежные вопросы