r - How to make a contingency table where one variable is categorized based on given breaks -
given data.frame following
id card.type mount 001 basic 500 002 basic 400 003 basic 700 004 basic 1000 005 silver 1200 006 silver 1300 007 basic 800 008 silver 1400 009 gold 2500 0010 gold 5000 0012 gold 7000 0013 gold 15000
i want create contingency table values of mount
categorized intervals based on vector b=c(0,100,500,1000,2000,3000,4000,5000)
. result table this:
card.type 0-100 101-500 501-1000 1001-2000 2001-3000 3001-4000 4001-5000 >5000 basic 0 2 2 0 0 0 0 0 silver 0 0 0 3 0 0 0 0 gold 0 0 0 0 1 0 1 2
i trying data.table
solution not able result. how can make table or possible add option when calling table()
function result?
df <- read.table(text="id card.type mount 001 basic 500 002 basic 400 003 basic 700 004 basic 1000 005 silver 1200 006 silver 1300 007 basic 800 008 silver 1400 009 gold 2500 0010 gold 5000 0012 gold 7000 0013 gold 15000",header=true) df$inter <- cut(df$mount,c(-1,100,500,1000,2000,3000,4000,5000,inf)) table(df[,c(2,4)]) # card.type (-1,100] (100,500] (500,1e+03] (1e+03,2e+03] (2e+03,3e+03] (3e+03,4e+03] (4e+03,5e+03] (5e+03,inf] # basic 0 2 3 0 0 0 0 0 # gold 0 0 0 0 1 0 1 2 # silver 0 0 0 3 0 0 0 0
Comments
Post a Comment