-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_utils.R
More file actions
43 lines (40 loc) · 1.1 KB
/
data_utils.R
File metadata and controls
43 lines (40 loc) · 1.1 KB
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
'%strin%' <- function(x, table, ignore.case = T){
unlist(sapply(x, function(i) {any(grepl(pattern = i, table, ignore.case = ignore.case))}))
}
corner <- function(mat, n = c(5,5), pos = 'top-left'){
if(length(n) == 1) n <- rep(n, 2)
nr <- min(n, nrow(mat))
nc <- min(n, ncol(mat))
ava_pos <- levels(interaction(c('top', 'middle', 'bottom'),
c('left', 'right', 'center'),
sep = '-', lex.order = T))
if(pos %in% ava_pos){
if('top' %strin% pos){
nrs <- 1
nre <- nr+nrs-1
}
if('middle' %strin% pos){
nrs <- round((nrow(mat) - nr)/2) +1
nre <- nr+nrs-1
}
if('bottom' %strin% pos){
nre <- nrow(mat)
nrs <- nre-nr+1
}
if('left' %strin% pos){
ncs <- 1
nce <- nc+ncs-1
}
if('center' %strin% pos){
ncs <- round((ncol(mat) - nc)/2) +1
nce <- nc+ncs-1
}
if('right' %strin% pos){
nce <- ncol(mat)
ncs <- nce-nc+1
}
}else{
stop(paste0('available position is ', paste0(ava_pos, collapse = ", ")))
}
return(mat[nrs:nre, ncs:nce])
}