r - Count of samples and presenting results based on several attributes within data -
this question has answer here:
- frequency counts in r 2 answers
i have data frame of attributes associated loans have been approved, pending review or have been declined. 1 of attribute fields in data frame town loan application originated. to create new matrix has town names in rows , count of how many loans have been approved, pending review or declined in columns. here 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 able arrange counts of loans status , town using table "for" each status type , 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, find more elegant , shorter way obtain same result using or command.
use table function, whole columns:
table(data$town, data$status) i think easiest way.
Comments
Post a Comment