-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.R
60 lines (53 loc) · 1.45 KB
/
utils.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
55
56
57
58
59
60
#### utils
# Author: Seungchan An
# function tocb()
# copies vectors into line-separated list
tocb <- function(x) {
x <- paste(x, collapse = "\n")
cat(x)
writeClipboard(x)
}
# function fromcb()
# returns a vector from line-separated list
fromcb <- function() {
x <- readClipboard()
return(unlist(strsplit(x, split = "\n")))
}
# function ul()
# returns length of unique elements of a vector
ul <- function(x) {
return(length(unique(x)))
}
# function htable()
# returns a head or tail of ordered frequency table
htable <- function(x, n = 6) {
tab <- table(x)
tab <- tab[order(tab, decreasing = TRUE)]
if(n > 0) {
tab <- head(tab, n)
} else {
tab <- tail(tab, -n)
}
return(tab)
}
# function pbar()
pbar <- function() {
# library(progress)
l <- 10 # length of for loop
pb <- progress_bar$new(format = "Progress: [:bar] :percent, Elapsed time :elapsedfull",
total = l, width = 80, clear = F, force = T)
# add tick in for loop
for(n in 1:l) {
pb$tick()
}
}
# function list2df()
# returns data.frame from the named list input
list2df <- function(l) {
# Create the data frame
df <- data.frame(
value = unlist(l), # Flatten all values
group = rep(names(l), times = lengths(l)) # Repeat names
)
df
}