I HAVE :
A1 <- c(1, 1, 0, 93, 19, 8.7)
A2 <- c(2, 1, 0, 226, 45, 6.9)
A3 <- c(3, 2, 1, 86, 17, 8.5)
B1 <- c(1, 4, 1, 113, 23, 7 )
B2 <- c(2, 4, 0, 108, 22, 7 )
B3 <- c(3, 5, 0, 246, 49, 9.5)
C1 <- c(1, 9, 0, 127, 25, 8 )
C2 <- c(2, 10, 1, 116, 23, 8 )
C3 <- c(3, 10, 0, 159.3, 32, 8 )
D1 <- c(1, 11, 0, 431, 86, 10 )
D2 <- c(2, 12, 0, 52, 10, 8 )
D3 <- c(3, 12, 1, 171, 34, 4.2)
E1 <- c(1, 16, 0, 270, 54, 7 )
E2 <- c(2, 16, 0, 86, 17, 8 )
E3 <- c(3, 16, 1, 237, 47, 9 )
F1 <- c(1, 19, 1, 98, 20, 8 )
F2 <- c(2, 20, 1, 164, 33, 8 )
F3 <- c(3, 21, 0, 99, 25, 8 )
G1 <- c(1, 22, 1, 125, 25, 9 )
G2 <- c(2, 25, 0, 222, 44, 9 )
G3 <- c(3, 25, 0, 161, 32, 11 )
H1 <- c(1, 26, 1, 52, 10, 6.8)
H2 <- c(2, 28, 0, 405, 81, 9 )
H3 <- c(3, 29, 0, 71, 14, 8 )
DG = matrix( c(A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,D3,E1,E2,E3,F1,F2,F3,G1,G2,G3,H1,H2,H3), nrow=6, ncol=24 , dimnames=list( c("number","time","status","Trig","vldl","low"), c("A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2","D3","E1","E2","E3","F1","F2","F3","G1","G2","G3","H1","H2","H3")))
TD <- t(DG)
want make combination between row without repeat as :
lapply(combn(24,3,simplify=FALSE),function(x) TD[x,]) # the some result will appear as :
[[1901]]
number time status Trig vldl low
E2 2 16 0 86 17 8
G3 3 25 0 161 32 11
H3 3 29 0 71 14 8
[[1902]]
number time status Trig vldl low
E2 2 16 0 86 17 8.0
H1 1 26 1 52 10 6.8
H2 2 28 0 405 81 9.0
[[1903]]
number time status Trig vldl low
E2 2 16 0 86 17 8.0
H1 1 26 1 52 10 6.8
H3 3 29 0 71 14 8.0
[[1904]]
number time status Trig vldl low
E2 2 16 0 86 17 8
H2 2 28 0 405 81 9
H3 3 29 0 71 14 8
[[1905]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
F2 2 20 1 164 33 8
[[1906]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
F3 3 21 0 99 25 8
[[1907]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
G1 1 22 1 125 25 9
[[1908]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
G2 2 25 0 222 44 9
[[1909]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
G3 3 25 0 161 32 11
[[1910]]
number time status Trig vldl low
E3 3 16 1 237 47 9.0
F1 1 19 1 98 20 8.0
H1 1 26 1 52 10 6.8
[[1911]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
H2 2 28 0 405 81 9
[[1912]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
H3 3 29 0 71 14 8
[[1913]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F2 2 20 1 164 33 8
F3 3 21 0 99 25 8
number time status Trig vldl low
G2 2 25 0 222 44 9.0
H1 1 26 1 52 10 6.8
H3 3 29 0 71 14 8.0
[[2020]]
number time status Trig vldl low
G2 2 25 0 222 44 9
H2 2 28 0 405 81 9
H3 3 29 0 71 14 8
[[2021]]
number time status Trig vldl low
G3 3 25 0 161 32 11.0
H1 1 26 1 52 10 6.8
H2 2 28 0 405 81 9.0
[[2022]]
number time status Trig vldl low
G3 3 25 0 161 32 11.0
H1 1 26 1 52 10 6.8
H3 3 29 0 71 14 8.0
[[2023]]
number time status Trig vldl low
G3 3 25 0 161 32 11
H2 2 28 0 405 81 9
H3 3 29 0 71 14 8
[[2024]]
number time status Trig vldl low
H1 1 26 1 52 10 6.8
H2 2 28 0 405 81 9.0
H3 3 29 0 71 14 8.0
this above some result form R but i want combination between row without repeat in letter or all matirix have semi letters as :
true resut :
[[1901]]
number time status Trig vldl low
E2 2 16 0 86 17 8
G3 3 25 0 161 32 11
H3 3 29 0 71 14 8
[[1908]]
number time status Trig vldl low
E3 3 16 1 237 47 9
F1 1 19 1 98 20 8
G2 2 25 0 222 44 9
FALSE RESULT :
[[2021]]
number time status Trig vldl low
G3 3 25 0 161 32 11.0
H1 1 26 1 52 10 6.8
H2 2 28 0 405 81 9.0
[[2023]]
number time status Trig vldl low
G3 3 25 0 161 32 11
H2 2 28 0 405 81 9
H3 3 29 0 71 14 8
[[2024]]
number time status Trig vldl low
H1 1 26 1 52 10 6.8
H2 2 28 0 405 81 9.0
H3 3 29 0 71 14 8.0
PLEASE HELP ME , can remove the repeat from combination as (H1,H2,H3) OR ( A1,A2,A3) OR (H1,A1,A2) ...... Etc, THANK U
You can generate all combinations of blocks (A, B, ...) first, and then enumerate all the combinations of rows for each combination of block:
rowidx <- as.matrix(do.call(expand.grid, rep(list(1:3), 3)))
gid <- cumsum(rep(3, 8)) - 3
res <- lapply(combn(1:8, 3, simplify = FALSE), function(x){
lapply(1:nrow(rowidx), function(y){
TD[gid[x] + rowidx[y, ], ]
})
})
res <- unlist(res, recursive = FALSE)
# sample example output
sample(res, 3)
# [[1]]
# number time status Trig vldl low
# A3 3 2 1 86 17 8.5
# E1 1 16 0 270 54 7.0
# G1 1 22 1 125 25 9.0
#
# [[2]]
# number time status Trig vldl low
# C1 1 9 0 127 25 8.0
# F2 2 20 1 164 33 8.0
# H1 1 26 1 52 10 6.8
#
# [[3]]
# number time status Trig vldl low
# B3 3 5 0 246 49 9.5
# C2 2 10 1 116 23 8.0
# E3 3 16 1 237 47 9.0
#
A generalized version:
gid <- cumsum(rep(3, 8)) - 3
n <- 4 # change n to whatever value (between 2..8) you want.
rowidx <- as.matrix(do.call(expand.grid, rep(list(1:3), n)))
res <- lapply(combn(1:8, n, simplify = FALSE), function(x){
lapply(1:nrow(rowidx), function(y){
TD[gid[x] + rowidx[y, ], ]
})
})
res <- unlist(res, recursive = FALSE)
Related
I want to substract the result for level time0 from the results from all other levels, for each id.
id <- rep(1:4,each=4)
time <- rep(c(0,5,10,15),4)
a <- c(34,56,67,35)
b <-c(56,78,23,90)
c <- c(23,89,67,78)
df <- data.frame(id,time,a,b,c)
df
id time a b c
1 1 0 34 56 23
2 1 5 56 78 89
3 1 10 67 23 67
4 1 15 35 90 78
5 2 0 34 56 23
6 2 5 56 78 89
7 2 10 67 23 67
8 2 15 35 90 78
9 3 0 34 56 23
10 3 5 56 78 89
11 3 10 67 23 67
12 3 15 35 90 78
13 4 0 34 56 23
14 4 5 56 78 89
15 4 10 67 23 67
16 4 15 35 90 78
I started like this but it feels there must be a more efficient way. Any suggestions? Thanks!
for( i in 1:length(unique(df$id))){
df_id <- df[df$id==i,]
for(j in 2:length(time)){
test <- t(df_id[,-1])
test[,c(2:4)]-test[,1]
}
Here's an option with dplyr -
library(dplyr)
df %>%
group_by(id) %>%
mutate(across(a:c, ~. - .[time == 0])) %>%
ungroup
# id time a b c
# <int> <dbl> <dbl> <dbl> <dbl>
# 1 1 0 0 0 0
# 2 1 5 22 22 66
# 3 1 10 33 -33 44
# 4 1 15 1 34 55
# 5 2 0 0 0 0
# 6 2 5 22 22 66
# 7 2 10 33 -33 44
# 8 2 15 1 34 55
# 9 3 0 0 0 0
#10 3 5 22 22 66
#11 3 10 33 -33 44
#12 3 15 1 34 55
#13 4 0 0 0 0
#14 4 5 22 22 66
#15 4 10 33 -33 44
#16 4 15 1 34 55
Using time == 0 would work if it is guaranteed that every id has exactly 1 value of time = 0. If for some id's there is no row for time = 0 or have more than one row with time = 0 then probably using match is better option.
df %>% group_by(id) %>% mutate(across(a:c, ~. - .[match(0, time)]))
Use mapply in by.
vc <- c('a', 'b', 'c')
by(df, df$id, \(x) {x[-1, vc] <- mapply(`-`, x[-1, vc], x[1, vc]);x}) |>
do.call(what=rbind)
# id time a b c
# 1.1 1 0 34 56 23
# 1.2 1 5 22 22 66
# 1.3 1 10 33 -33 44
# 1.4 1 15 1 34 55
# 2.5 2 0 34 56 23
# 2.6 2 5 22 22 66
# 2.7 2 10 33 -33 44
# 2.8 2 15 1 34 55
# 3.9 3 0 34 56 23
# 3.10 3 5 22 22 66
# 3.11 3 10 33 -33 44
# 3.12 3 15 1 34 55
# 4.13 4 0 34 56 23
# 4.14 4 5 22 22 66
# 4.15 4 10 33 -33 44
# 4.16 4 15 1 34 55
If id==0 position is not consistent, you need to formulate more verbose:
{x[x$time != 0, vc] <- mapply(`-`, x[x$time != 0, vc], x[x$time == 0, vc]);x}
Data:
df <- structure(list(id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L), time = c(0, 5, 10, 15, 0, 5, 10, 15,
0, 5, 10, 15, 0, 5, 10, 15), a = c(34, 56, 67, 35, 34, 56, 67,
35, 34, 56, 67, 35, 34, 56, 67, 35), b = c(56, 78, 23, 90, 56,
78, 23, 90, 56, 78, 23, 90, 56, 78, 23, 90), c = c(23, 89, 67,
78, 23, 89, 67, 78, 23, 89, 67, 78, 23, 89, 67, 78)), class = "data.frame", row.names = c(NA,
-16L))
I've got a dataframe that looks something like this:
time <- c(1:250)
trial <- rep(1, 250)
X <- sample(200, 250, replace=TRUE)
Y <- sample(200, 250, replace=TRUE)
follow <- c(rep(0, 20), rep(1, 25), rep(0, 5), rep(1, 50), rep(0, 55), rep(1, 30), rep(0, 35), rep(1, 10), rep(0, 20))
df <- as.data.frame(cbind(trial, time, X, Y, follow))
I've got a vector called follow, which just acts as a label for when a certain behavior is being exhibited. However, I'd like each instance of when 1 appears to be unique, like this:
time <- c(1:250)
trial <- rep(1, 250)
X <- sample(200, 250, replace=TRUE)
Y <- sample(200, 250, replace=TRUE)
follow <- c(rep(0, 20), rep(1, 25), rep(0, 5), rep(2, 50), rep(0, 55), rep(3, 30), rep(0, 35), rep(4, 10), rep(0, 20))
df <- as.data.frame(cbind(trial, time, X, Y, follow))
Is there a good way to do this? Thanks all!
A tidyverse option.
library(dplyr)
library(tidyr)
mutate(df, follow = if_else(follow == 1,
cumsum(replace_na(lag(follow) < follow, 0)),
0))
# trial time X Y follow
# 1 1 1 188 48 0
# 2 1 2 53 122 0
# 3 1 3 197 191 0
# 4 1 4 123 50 0
# 5 1 5 18 142 0
# 6 1 6 36 29 0
# 7 1 7 101 111 0
# 8 1 8 91 10 0
# 9 1 9 185 36 0
# 10 1 10 72 83 0
# 11 1 11 138 94 0
# 12 1 12 193 25 0
# 13 1 13 34 17 0
# 14 1 14 51 162 0
# 15 1 15 141 106 0
# 16 1 16 99 193 0
# 17 1 17 77 58 0
# 18 1 18 96 12 0
# 19 1 19 25 119 0
# 20 1 20 173 17 0
# 21 1 21 78 141 1
# 22 1 22 73 184 1
# 23 1 23 145 103 1
# 24 1 24 161 36 1
# 25 1 25 38 99 1
# 26 1 26 56 178 1
# 27 1 27 39 95 1
# 28 1 28 109 31 1
# 29 1 29 184 119 1
# 30 1 30 84 132 1
# 31 1 31 103 150 1
# 32 1 32 177 80 1
# 33 1 33 153 129 1
# 34 1 34 121 25 1
# 35 1 35 184 158 1
# 36 1 36 190 16 1
# 37 1 37 29 165 1
# 38 1 38 102 51 1
# 39 1 39 105 17 1
# 40 1 40 71 5 1
# 41 1 41 10 5 1
# 42 1 42 160 95 1
# 43 1 43 143 96 1
# 44 1 44 101 11 1
# 45 1 45 154 127 1
# 46 1 46 20 133 0
# 47 1 47 199 192 0
# 48 1 48 83 3 0
# 49 1 49 22 96 0
# 50 1 50 86 57 0
# 51 1 51 163 87 2
I have the following simple linear programming model that I am solving with the linprog package in R:
install.packages("linprog")
library(linprog)
function_opt <- c(8, 13, 9, 8, 9, 11, 12, 10, 7, 8, 10, 9)
names(function_opt) <- c("F1A1","F1A2","F1A3","F1A4","F2A1","F2A2","F2A3","F2A4","F3A1","F3A2","F3A3","F3A4")
##Order: 3 factory capacities, 4 customer demands
cons_indep_term <- c(60, 70, 80, 75, 45, 40, 50)
names(cons_indep_term) <- c("F1","F2","F3","A1","A2","A3","A4")
r1 <- c(1,1,1,1,0,0,0,0,0,0,0,0)
r2 <- c(0,0,0,0,1,1,1,1,0,0,0,0)
r3 <- c(0,0,0,0,0,0,0,0,1,1,1,1)
r4 <- c(1,0,0,0,1,0,0,0,1,0,0,0)
r5 <- c(0,1,0,0,0,1,0,0,0,1,0,0)
r6 <- c(0,0,1,0,0,0,1,0,0,0,1,0)
r7 <- c(0,0,0,1,0,0,0,1,0,0,0,1)
cons_coef <- rbind(r1,r2,r3,r4,r5,r6,r7)
res <- solveLP(function_opt, cons_indep_term, cons_coef, maximum=FALSE, const.dir = c("<=","<=","<=",">=",">=",">=",">="))
print (res)
The sixth constraint requires that the sum of the FxA3 variables must be at least 40. However, the solution that comes out as:
Results of Linear Programming / Linear Optimization
Objective function (Minimum): 1355
Iterations in phase 1: 6
Iterations in phase 2: 3
Solution
opt
F1A1 10
F1A2 0
F1A3 0
F1A4 50
F2A1 30
F2A2 0
F2A3 0
F2A4 0
F3A1 35
F3A2 45
F3A3 0
F3A4 0
Basic Variables
opt
F1A1 10
F1A4 50
F2A1 30
F3A1 35
F3A2 45
S F2 40
S A3 40
Constraints
actual dir bvec free dual dual.reg
F1 60 <= 60 0 1 10
F2 30 <= 70 40 0 40
F3 80 <= 80 0 2 35
A1 75 >= 75 0 9 40
A2 45 >= 45 0 10 35
A3 80 >= 40 40 0 40
A4 50 >= 50 0 9 10
All Variables (including slack variables)
opt cvec min.c max.c marg marg.reg
F1A1 10 8 -9 9 NA NA
F1A2 0 13 99 77 4 10
F1A3 0 9 99 77 10 10
F1A4 50 8 -17 9 NA NA
F2A1 30 9 -10 10 NA NA
F2A2 0 11 99 77 1 30
F2A3 0 12 99 77 12 40
F2A4 0 10 99 77 1 30
F3A1 35 7 -8 9 NA NA
F3A2 45 8 -18 9 NA NA
F3A3 0 10 99 77 12 35
F3A4 0 9 99 77 2 35
S F1 0 0 -1 Inf 1 10
S F2 40 0 NA 1 0 NA
S F3 0 0 -2 Inf 2 35
S A1 0 0 -9 Inf 9 40
S A2 0 0 -10 Inf 10 35
S A3 40 0 NA 10 0 NA
S A4 0 0 -9 Inf 9 10
All three FxA3 variables are set to 0, meaning the sixth constraint is violated. What is the problem? I have triplechecked everything but still no idea.
This is... very weird, and I can't spot any issue in your code. Since this is such a simple LP, you might think about filing a bug to the package maintainers.
That being said, you should be able to get unblocked by using the lpSolve package, which has a nearly identical interface (and is, in fact, used by the linprog package you asked about):
library(lpSolve)
mod2 = lp(direction = "min",
objective.in = function_opt,
const.mat = cons_coef,
const.dir = c("<=","<=","<=",">=",">=",">=",">="),
const.rhs = cons_indep_term)
setNames(mod2$solution, names(function_opt))
# F1A1 F1A2 F1A3 F1A4 F2A1 F2A2 F2A3 F2A4 F3A1 F3A2 F3A3 F3A4
# 0 0 40 20 40 0 0 30 35 45 0 0
mod2$objval
# [1] 1785
In the optimal solution, we have F1A3 = 40, F2A3 = 0, and F3A3 = 0, so the sixth constraint is met.
Here is a sample data frame:
> df = data.frame(rep(seq(0, 120, length.out=6), times = 2), c(sample(1:50, 4),
+ NA, NA, NA, sample(1:50, 5)))
> colnames(df) = c("Time", "Pat1")
> df
Time Pat1
1 0 33
2 24 48
3 48 7
4 72 8
5 96 NA
6 120 NA
7 0 NA
8 24 1
9 48 6
10 72 28
11 96 31
12 120 32
NAs which have to be replaced are identified by which and logical operators:
x = which(is.na(df$Pat1) & df$Time == 0)
I know the locf() command, but it's replacing all NAs. How can I replace only the NAs at position x in a multi-column df?
EDIT: Here is a link to my original dataset: link
And thats how far I get:
require(reshape2)
require(zoo)
pad.88 <- read.csv2("pad_88.csv")
colnames(pad.88) = c("Time", "Increment", "Side", 4:length(pad.88)-3)
attach(pad.88)
x = which(Time == 240 & Increment != 5)
pad.88 = pad.88[c(1:x[1], x[1]:x[2], x[2]:x[3], x[3]:x[4], x[4]:x[5], x[5]:x[6],x[6]:x[7], x[7]:x[8], x[8]:nrow(pad.88)),]
y = which(duplicated(pad.88))
pad.88$Time[y] = 0
pad.88$Increment[y] = Increment[x] + 1
z = which(is.na(pad.88[4:ncol(pad.88)] & pad.88$Time == 0), arr.ind=T)
a = na.locf(pad.88[4:ncol(pad.88)])
My next step is something like pat.cols[z] = a[z], which doesn't work.
That's how the result should look like:
Time Increment Side 1 2 3 4 5 ...
150 4 0 27,478 24,076 27,862 20,001 25,261
165 4 0 27,053 24,838 27,231 20,001 NA
180 4 0 27,599 24,166 27,862 20,687 NA
195 4 0 27,114 23,403 27,862 20,001 NA
210 4 0 26,993 24,076 27,189 19,716 NA
225 4 0 26,629 24,21 26,221 19,887 NA
240 4 0 26,811 26,228 26,431 20,001 NA
0 5 1 26,811 26,228 26,431 20,001 25,261
15 5 1 ....
The last valid value in col 5 is 25,261. This value replaces the NA at Time 0/Col 5.
You can change it so that x records all the NA values and use the first and last from that to identify the locations you want.
df
Time Pat1
1 0 36
2 24 13
3 48 32
4 72 38
5 96 NA
6 120 NA
7 0 NA
8 24 5
9 48 10
10 72 7
11 96 25
12 120 28
x <- which(is.na(df$Pat1))
df[rev(x)[1],"Pat1"] <- df[x[1]-1,"Pat1"]
df
Time Pat1
1 0 36
2 24 13
3 48 32
4 72 38
5 96 NA
6 120 NA
7 0 38
8 24 5
9 48 10
10 72 7
11 96 25
12 120 28
For the multi-column example use the same idea in a sapply call:
cbind(df[1],sapply(df[-1],function(x) {y<-which(is.na(x));x[rev(y)[1]]<-x[y[1]-1];x}))
Time Pat1 Pat2
1 0 41 42
2 24 8 30
3 48 3 41
4 72 14 NA
5 96 NA NA
6 120 NA NA
7 0 14 41
8 24 5 37
9 48 29 48
10 72 31 11
11 96 50 43
12 120 46 21
I am playing around to develop a sampling function to do randomization to make days easier:
Question:
pln <- 1:80
bcap <- cumsum(c(20, 12, 16, 16, 16))
bcap
[1] 20 32 48 64 80
I want to randomize pln such that 1:20, 21:32, 33:48, 49:64, 65:80, for this example. This might vary for different scenarios.
newpln <- c(sample(1:20), sample(21:32), sample(33:48),
sample(49:64), sample(65:80))
I want create a general function where length of bcap can be of any number, however the pln should run 1: max(bcap).
Is this what you want?
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
[1] 13 19 4 16 11 2 5 20 9 14 10 3 1 7 6 8 17 12 15 18 27 24 30 32 23 25 28 21 31 26 29 22 39 41 48 36 37 45 42 47 43 38 40 34 35
[46] 44 46 33 60 52 50 58 51 54 62 55 64 61 59 49 63 53 56 57 72 74 76 78 67 69 70 66 73 79 68 80 77 71 75 65
Testing:
> pln <- 1:12
> pln
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> bcap <- cumsum(c(4, 3, 2, 3))
> bcap
[1] 4 7 9 12
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
[1] 4 2 3 1 6 5 7 8 9 12 11 10
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
[1] 4 2 3 1 6 5 7 9 8 10 12 11
> unlist(sapply(mapply(seq, c(1, bcap[1:(length(bcap)-1)]+1), bcap), sample))
[1] 2 3 1 4 7 6 5 8 9 11 10 12
You can do this with one call to mapply. You just need an object that contains what's inside the cumsum call of your bcap object.
bvec <- c(20, 12, 16, 16, 16)
mapply(function(x,y) sample(x)+y-x, bvec, cumsum(bvec))
A small example:
bvec <- c(2,1,3,1)
set.seed(21)
unlist(mapply(function(x,y) sample(x)+y-x, bvec, cumsum(bvec)))
# [1] 2 1 3 4 5 6 7
library("plyr")
unlist(
llply(
mlply(
data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),
seq),
sample),
use.names = FALSE)
Make a data.frame with each ranges from/to, use that to make a list with the sequences, sample each list, and then combine them together.
UPDATE:
worked for me:
> library("plyr")
> bcap <- cumsum(c(4, 3, 2, 3))
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
[1] 4 2 3 1 7 4 5 6 9 7 8 12 9 11 10
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
[1] 3 1 2 4 5 6 4 7 9 7 8 9 12 10 11
> unlist(llply(mlply(data.frame(from=c(1,bcap[-length(bcap)]), to=bcap),seq),sample),use.names=FALSE)
[1] 2 3 4 1 6 5 4 7 8 9 7 11 10 12 9