0

I am trying to nest two subset data inside aggregate. but it returns different length of data and hence unable to compute. What options do i have?

newfile<- aggregate(cbind(subset(IVA,IVA$IVR.Agent =="Agent"),subset(IVA,IVA$IVR.Agent=="IVR")) ~ IVA$OrderNo, FUN = length, rm.action = TRUE)

Below is the error that I got

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 25039, 4585

I am trying to make a pivot table that will show Agent count and IVR count in separate columns just like in excel, if for a specific order there is no IVR it shows null otherwise its count.

DataSet looks like this

OrderNo IVR/Agent
    A1  IVR
    B1  Agent
    A2  Agent
    B2  IVR
    A3  Agent
    B3  Agent
    A4  Agent
    B4  Agent
    A5  IVR
    B5  Agent

Pivot

Shoaibkhanz
  • 1,942
  • 3
  • 24
  • 41
  • 2
    It's really difficult (actually impossible) to give you any answer without displaying how your data is structured. However, my preferred method for pivot tables is `ddply` in the `plyr` package or if you're familiar with `dplyr` try `group_by` – maloneypatr Mar 24 '14 at 11:26
  • 1
    Please review the [guidelines for posting a minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – hrbrmstr Mar 24 '14 at 11:28
  • I have just added the structure of dataset above. – Shoaibkhanz Mar 24 '14 at 11:36

2 Answers2

2

If your data is called myData the following seems to be what you need.

table(myData)

# which yields

       IVR.Agent
OrderNo Agent IVR
     A1     0   1
     A2     1   0
     A3     1   0
     A4     1   0
     A5     0   1
     B1     1   0
     B2     0   1
     B3     1   0
     B4     1   0
     B5     1   0

Just add up the columns for a grand total if you want one.

gt <- rowSums( table(s) )

or

tab <- table(s)
gt <- tab[,1] + tab[,2]
John
  • 23,360
  • 7
  • 57
  • 83
1

So, you wanna check out the reshape2 package and the cast function. Check out the code below:

# install.packages('reshape2') # only run if you haven't downloaded before
library(reshape2)

datCast <- dcast(dat, OrderNo ~ IVR.Agent, fun.aggregate = length, margins = T)

datCast
   OrderNo Agent IVR (all)
1       A1     0   1     1
2       A2     1   0     1
3       A3     1   0     1
4       A4     1   0     1
5       A5     0   1     1
6       B1     1   0     1
7       B2     0   1     1
8       B3     1   0     1
9       B4     1   0     1
10      B5     1   0     1
11   (all)     7   3    10
maloneypatr
  • 3,562
  • 4
  • 23
  • 33