使用一组断点将数据分成分位数

时间:2019-05-19 01:42:00

标签: r datatable quantile

对于每个级别的“ ts_id”,如何使用ex_id == 1的行计算出的中值“ s1”将所有数据分成2个bin?

DT = data.table(

     ts_id = c(rep(1,15),rep(2,15)),
     ex_id = c(rep(1,5),rep(2,10),rep(1,5),rep(2,10)),
     s1 = rnorm(30)

)

1 个答案:

答案 0 :(得分:0)

May be this would help. Specify the i with logical expression (ex_id == 1), grouped by 'ts_id', split the 's1' by a logical vector created by comparing the values of 's1' with the median of 's1' (here, it is changed to numeric index)

DT[ex_id==1,  list(split(s1, (s1 >= median(s1)) + 1)), ts_id]

NOTE: The expected output is not clear