Still difficult to figure out what you are trying to accomplish, but this is my best guess:
# create reproducible example - you already have this...
set.seed(1)
df <- data.frame(Y=sample(0:1,100000,replace=T),
TOTA=runif(100000,0,18938000))
na <- sample(1:100000,5000) # 5% NA
df[na,]$TOTA <- NA
# you start here...
breaks <- c(0,10^(2:6), 5938000, 10938000, 15938000, 18938000)
labels <- c("0-100","100-1000","1000-10000","10000-100000",
"100000-100000","100000-1000000","1000000-5938000",
"5938000-10938000","10938000-18938000","NA")
df$group <- cut(df$TOTA,breaks=breaks,labels=F)
df[is.na(df$group),]$group <- 10
df$grpLabel <- labels[df$group]
result <- aggregate(Y~group,df,function(x)sum(x==1))
colnames(result) <- c("Group","Active")
result$Passive <- aggregate(Y~group,df,function(x)sum(x==0))$Y
result$Group <- labels[result$Group]
result
# Group Active Passive
# 1 0-100 0 1
# 2 100-1000 1 2
# 3 1000-10000 29 17
# 4 10000-100000 224 212
# 5 100000-100000 2310 2288
# 6 100000-1000000 12365 12328
# 7 1000000-5938000 12508 12522
# 8 5938000-10938000 12526 12649
# 9 10938000-18938000 7485 7533
# 10 NA 2544 2456
So this divides the dataset into groups using cut(...)
, then sums the 1s
and 0s
separately using aggregate(...)
, then labels the groups. Normally you could use cut(...)
without labels=F
and get meaningful labels for your groups directly. The problem here is that aggregate(...)
will sort these alphabetically, which is not what you want.
Also, note that in your question you have a range 1000000 - 1000000 (e.g 1MM to 1MM). I assumed this is supposed to be 1000000 - 5938000.