-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.R
executable file
·54 lines (40 loc) · 1.23 KB
/
misc.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## this doesn't exactly change the default... afterall, defaults can be overridden
jCat <- function(...) { cat(...,sep="",fill=TRUE) }
jPaste <- function(...) {paste(..., sep="")}
jSeq <- function(a,b) if (a<=b) return(seq(a,b)) else return(NULL)
##output: list of rows
rows <- function(mat) {
l <- list()
for(i in 1:nrow(mat)){
l[[i]] <- mat[i,]
}
l
}
## Use as.list(data.frame(combn(1:5,2))) instead. (NOT TESTED)
##output: list of columns
columns <- function(mat) {
l <- list()
for(j in 1:ncol(mat)){
l[[j]] <- mat[,j]
}
l
}
##unlike 'match', returns multiple matches
find <- function(elt,l){
seq_along(l)[l==elt]
}
##best used in combination with 'cumsum', (See also: 'rmultinom')
## findLessThan(orderedVector)
## scientific notation
form <- function(x) format(x, scientific=TRUE, digits=3)
## Filter, rathtr than ## select <- function(test, v) v[which(test(v))]
is.odd <- function(n) n%%2 == 1
Filter(not(is.odd), 1:5)
## not <- function(test) function(x) !test(x)
##
## removeNA <- function(v) Filter(not(is.na), v)
##returns indices
which.topk <- function(v, n){
threshold <- sort(v, decreasing=TRUE)[n]
which(v>=threshold)[1:n] ## randomize this
}