How to classify output variable in different levels of output variable

r
classification

#1

I am currently doing one classification problem and while doing classification I have to build output in each different level of output variable but I am not able to perform.

my data

'data.frame':   878049 obs. of  9 variables:
 $ Dates     : Factor w/ 389257 levels "2003-01-06 00:01:00",..: 389257 389257 389256 389255 389255 389255 389255 389255 389254 389254 ...
 $ Category  : Factor w/ 39 levels "ARSON","ASSAULT",..: 38 22 22 17 17 17 37 37 17 17 ...
 $ Descript  : Factor w/ 879 levels "ABANDONMENT OF CHILD",..: 867 811 811 405 405 407 740 740 405 405 ...
 $ DayOfWeek : Factor w/ 7 levels "Friday","Monday",..: 7 7 7 7 7 7 7 7 7 7 ...
 $ PdDistrict: Factor w/ 10 levels "BAYVIEW","CENTRAL",..: 5 5 5 5 6 3 3 1 7 2 ...
 $ Resolution: Factor w/ 17 levels "ARREST, BOOKED",..: 1 1 1 12 12 12 12 12 12 12 ...
 $ Address   : Factor w/ 23228 levels "0 Block of  HARRISON ST",..: 19791 19791 22698 4267 1844 1506 13323 18055 11385 17659 ...
 $ X         : num  -122 -122 -122 -122 -122 ...
 $ Y         : num  37.8 37.8 37.8 37.8 37.8 ...

the output variable is the category in which there is 39 levels and I want result like this.


#2

Hi Hinduja,

You will have to write a function to build such a table or few lines a of code anyway I would do this ways:

  1. Get the levels of the factors
    2 Build data.frame with namaes as labels
  2. Unclass the factors
  3. Do a map Unclass to a vector bu doing replicate of 0 and set to 1 at unclass
    4 Rbind to data frame

----------Example in R not optimised !!!

factordataframe <-function(myfactor){
namesclass <-levels(myfactor)
back <-as.data.frame(matrix(replicate(length(namesclass), 0),nrow=1 ))
names(back) <- namesclass
myfactor<-unclass(myfactor)
for( curfactor in myfactor){
myvector <-replicate(length(namesclass),0)
myvector[curfactor] <-1
back<-rbind(back, myvector)
}
back<-back[1,]
return(back)
}

Hope this help.
Alain


#3

Sorry
back<-back[-1,]
return(back)

at the end