-1

I have a dataset with various fileds and I want to aggregate it on 1 column and horizontally tabulize on other columns

Region  Device
a   ios
a   chrome
a   safari
a   ios
a   chrome
b   chrome
b   chrome
b   safari
c   ios
c   chrome
c   ios

I want the output to be like

Region  ios Chrome  safari
a   2   2   1
b   0   2   1
c   2   1   0

My end motive is to make tabular as well as graphical representations

kath
  • 7,624
  • 17
  • 32
Vaibhav
  • 21
  • 6
  • Region Device a ios a chrome a safari a ios a chrome b chrome b chrome b safari c ios c chrome c ios – Vaibhav Feb 23 '18 at 07:49
  • Try `table(df$Region, df$Device)` (if you data is stored in a dataframe called df) – kath Feb 23 '18 at 07:56
  • related: https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – jogo Feb 23 '18 at 08:18

1 Answers1

0

Using base R:

> dat
   Region Device
1       a    ios
2       a chrome
3       a safari
4       a    ios
5       a chrome
6       b chrome
7       b chrome
8       b safari
9       c    ios
10      c chrome
11      c    ios
> xtabs(s~Region+Device,cbind(dat,s=1))
      Device
Region chrome ios safari
     a      2   2      1
     b      2   0      1
     c      1   2      0

Using reshape2 package:

> library(reshape2)
> dcast(dat,Region~Device,length,value.var="Device")
  Region chrome ios safari
1      a      2   2      1
2      b      2   0      1
3      c      1   2      0
> 
Onyambu
  • 67,392
  • 3
  • 24
  • 53