Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 210797

On a column in a data.table, record "by = ..."-stratified and non-stratified outputs from a function in a single "data.table" way hit

$
0
0

I am recording an output from R quintile() function for a column of a data.table, stratified by "by = ...":

require(data.table)

DT <- data.table(iris)
tab_strata <- DT[,as.list(quantile(Sepal.Length)), by = Species]

      Species  0%   25% 50% 75% 100%
1:     setosa 4.3 4.800 5.0 5.2  5.8
2: versicolor 4.9 5.600 5.9 6.3  7.0
3:  virginica 4.9 6.225 6.5 6.9  7.9

I would also want to get the same output without "by = ...", i.e. computed on the entire column, and join it to the initial output so I have a single final table as follows:

tab_all <- DT[,as.list(quantile(Sepal.Length))]
tab_all <- cbind("Species" = "all", tab_all)
tab_result <- rbind(tab_all, tab_strata)

      Species  0%   25% 50% 75% 100%
1:        all 4.3 5.100 5.8 6.4  7.9
2:     setosa 4.3 4.800 5.0 5.2  5.8
3: versicolor 4.9 5.600 5.9 6.3  7.0
4:  virginica 4.9 6.225 6.5 6.9  7.9

My question: Is it possible to obtain tab_result with "all" and "by = ..." outcomes in a single "data.table" way hit?


Viewing all articles
Browse latest Browse all 210797

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>