Skip to content

Instantly share code, notes, and snippets.

@jokergoo
Last active December 19, 2015 21:59
Show Gist options
  • Save jokergoo/6024063 to your computer and use it in GitHub Desktop.
Save jokergoo/6024063 to your computer and use it in GitHub Desktop.
assignment = function(x, k = 4) {
names(x) = seq_along(x)
y = vector("list", k)
ysum = sapply(y, function(x) 0)
flag = 0
while(length(x)) {
if(flag) {
a = sort(x, decreasing = FALSE)[1:ifelse(length(x) > k, k, length(x))]
for(i in seq_along(a)) {
o = order(ysum, decreasing = TRUE)
y[[ o[i] ]] = c(y[[ o[i] ]], a[i])
}
x = x[ -order(x, decreasing = FALSE)[seq_along(a)] ]
flag = !flag
} else {
a = sort(x, decreasing = TRUE)[1:ifelse(length(x) > k, k, length(x))]
for(i in seq_along(a)) {
o = order(ysum, decreasing = FALSE)
y[[ o[i] ]] = c(y[[ o[i] ]], a[i])
}
x = x[ -order(x, decreasing = TRUE)[seq_along(a)] ]
flag = !flag
}
ysum = sapply(y, sum)
}
lapply(y, function(x) sort(as.numeric(names(x))))
}
chr.length = c(
chr1=249250621,
chr2=243199373,
chr3=198022430,
chr4=191154276,
chr5=180915260,
chr6=171115067,
chr7=159138663,
chr8=146364022,
chr9=141213431,
chr10=135534747,
chr11=135006516,
chr12=133851895,
chr13=115169878,
chr14=107349540,
chr15=102531392,
chr16=90354753,
chr17=81195210,
chr18=78077248,
chr19=59128983,
chr20=63025520,
chr21=48129895,
chr22=51304566,
chrX=155270560,
chrY=59373566)
assignment(chr.length)
assignment(chr.length, 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment