How to group results, and have the counts of each group written into a matrix in R? -


here's table :

month    d1       d2      d3     d4  1.      11149   10488    5593    3073 1.      6678    11073    789     10009 2.      2322    10899    3493    21 3.      5839    11563    4367    9987 

i want divide above content ( 4 columns of distance 4 rows of month) in 3 groups , have counts of each group written in matrix such :

month        d<=700    700<d<1000   d>=1000 1.           counts       counts    .... 2.           ...           3.           .... 

what's fastest way ??

thanks !

a solution using data.table package:

library(data.table) library(magrittr)  setdt(dt)[, cut(colsums(.sd),breaks=c(0,700,1000,max(colsums(.sd)))) %>%               table %>%               as.list           , month]  #   month (0,700] (700,1e+03] (1e+03,2.16e+04] #1:     1       0           0                4 #2:     2       1           0                3 #3:     3       0           0                4 

data:

dt = structure(list(month = c(1, 1, 2, 3), d1 = c(11149l, 6678l, 2322l,  5839l), d2 = c(10488l, 11073l, 10899l, 11563l), d3 = c(5593l,  789l, 3493l, 4367l), d4 = c(3073l, 10009l, 21l, 9987l)), .names = c("month",  "d1", "d2", "d3", "d4"), class = "data.frame", row.names = c(na,  -4l)) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -