1

I have a data frame of attributes associated with loans that have been approved, are pending review or have been declined. One of the attribute fields in the data frame is the town where the loan application originated. I would like to to create a new matrix that has all the town names in the rows and the count of how many loans have been approved, pending review or declined in the columns. Here is illustrative data:

Town<-c("Andover","Cheshire", "Andover", "Burlington", "Albany", "Cheshire")
Status<-c("Approved","Declined", "Pending", "Pending", "Approved", "Declined")
ApplicationYear<-c(2013,2015,2014,2014,2015,2013)
Data<-data.frame(cbind(Town,Status,ApplicationYear))

     Town   Status ApplicationYear
1    Andover Approved            2013
2   Cheshire Declined            2015
3    Andover  Pending            2014
4 Burlington  Pending            2014
5     Albany Approved            2015
6   Cheshire Declined            2013

I am able to arrange the counts of loans by status and by town using table "for" each status type and then cbind.

Approved<-table(Data[Data$Status=="Approved",]$Town)
Declined<-table(Data[Data$Status=="Declined",]$Town)
Pending<-table(Data[Data$Status=="Pending",]$Town)
cbind(Approved,Declined,Pending)

          Approved Declined Pending
Albany            1        0       0
Andover           1        0       1
Burlington        0        0       1
Cheshire          0        2       0

However, I would like to find a more elegant and shorter way to obtain the same result using for or another command.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Danny
  • 554
  • 1
  • 6
  • 17

1 Answers1

2

Use the table function, just with the whole columns:

table(Data$Town, Data$Status)

I think this is the easiest way.

Daniel
  • 278
  • 3
  • 13