R add many columns by a function -
i trying cut number in layers mean next code:
x <- matrix(c(6,7,9,9,9,17,19,4,12,2,3,6,7,7),ncol=2) layers <- c(5,10,15,20,25,30,35,40) partitions <- function(u) {cbind(pmin(layers[1],u),t(diff(pmin(layers,u))))} x <- cbind(x,lapply(x[,2], partitions))
the function returns integer partitioned in layers.
a = a1 + a2 + .... + a8
example
a <- 19 partitions(a) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 5 5 5 4 0 0 0 0
but results not have matrix need. final matrix of 7 x (2 (cols x) + 8 (num of layers in points))
[,1] [,2] [,3] [1,] 6 numeric,8 null [2,] 7 numeric,8 null [3,] 9 numeric,8 null [4,] 9 numeric,8 null [5,] 9 numeric,8 null [6,] 17 numeric,8 null [7,] 19 numeric,8 null > dim(x) [1] 7 3
i tried different forms , ever had errors of dimensions.
regards
one of these 2 should want
> rbind(t(x),sapply(x[,2], partitions)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 6 7 9 9 9 17 19 [2,] 4 12 2 3 6 7 7 [3,] 4 5 2 3 5 5 5 [4,] 0 5 0 0 1 2 2 [5,] 0 2 0 0 0 0 0 [6,] 0 0 0 0 0 0 0 [7,] 0 0 0 0 0 0 0 [8,] 0 0 0 0 0 0 0 [9,] 0 0 0 0 0 0 0 [10,] 0 0 0 0 0 0 0 > cbind(x,t(sapply(x[,2], partitions))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 4 4 0 0 0 0 0 0 0 [2,] 7 12 5 5 2 0 0 0 0 0 [3,] 9 2 2 0 0 0 0 0 0 0 [4,] 9 3 3 0 0 0 0 0 0 0 [5,] 9 6 5 1 0 0 0 0 0 0 [6,] 17 7 5 2 0 0 0 0 0 0 [7,] 19 7 5 2 0 0 0 0 0 0