-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotor.R
More file actions
executable file
·50 lines (36 loc) · 1.29 KB
/
rotor.R
File metadata and controls
executable file
·50 lines (36 loc) · 1.29 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
44
45
46
47
48
49
50
#!/bin/env Rscript
rotor <- function(x, size, partial = FALSE) {
# Create "label" for each element for which vector it will be placed in.
labels <- ceiling(seq_along(x) / size) # ceiling makes 1st "bin" 1, 2nd 2, etc
# Split vector into groups based on label
my_list <- split(x, labels)
# If partial not set, trim down to not include "partial"
if (! partial) {
# Calculate number of complete (non-partial) bins (%/% is integer division)
num_complete_bins <- length(x) %/% size
# Subset to not include the last bin
my_list <- my_list[1:num_complete_bins]
}
return(my_list)
}
# How do I export this thing?
test_rotor <- function() {
nums <- c(1:20, 90:110, 4:9)
my_lists <- rotor(nums, 10)
print(nums)
print("rotor without partial:")
print(my_lists)
print("Means of rotored list:")
means <- sapply(names(my_lists), function(x) mean(my_lists[[x]]))
print(means)
with_partial <- rotor(nums, 10, partial = TRUE)
print("rotored list with partial:")
print(with_partial)
print("means of rotored list with partial:")
print(sapply(names(with_partial), function(x) mean(with_partial[[x]])))
}
# modulino
if (length(sys.frames()) == 0) { # if true, this script has been run itself
print("Running directly from the command-line, so running test")
test_rotor()
}