Skip to content

Instantly share code, notes, and snippets.

View mrdwab's full-sized avatar

Ananda Mahto mrdwab

View GitHub Profile
@mrdwab
mrdwab / SO65151555.R
Last active December 6, 2020 00:01
Testing the different options shared at https://stackoverflow.com/q/65151555/1270695
## SETUP: Sample data and packages
library(data.table)
library(readr)
library(dplyr)
library(iotools)
n <- 5000
set.seed(1)
vals_row <- sample(2000, n, TRUE)
@mrdwab
mrdwab / row_wrap.R
Created December 4, 2020 01:00
Wraps rows into new rows, optionally including the source row. https://stackoverflow.com/q/65118797/1270695
row_wrap <- function(input, ...) {
UseMethod("row_wrap")
}
row_wrap.data.frame <- function(input, ncols, row_ind = FALSE) {
if (ncol(input) %% ncols != 0) stop("Number of columns not divisible by desired output")
data.frame(row_wrap.matrix(input, ncols, row_ind))
}
row_wrap.matrix <- function(input, ncols, row_ind = FALSE) {
#' Calculate the Mean of Already Grouped Data
#'
#' Calculates the mean of already grouped data given the interval ranges and
#' the frequencies of each group.
#'
#' @param frequencies A vector of frequencies.
#' @param intervals A 2-column `matrix` with the same number of rows as
#' the length of frequencies, with the first column being the lower class
#' boundary, and the second column being the upper class boundary.
#' Alternatively, `intervals` may be a character vector, and you may
#' All Factors of a Number
#'
#' @param x The number that you want to find the factors of.
#' @examples
#' factors_of(8)
#' @export
factors_of <- function(x) which(!x %% seq_len(x))
#' Common Factors of Multiple Numbers
#'
lengthen <- function(vec, length) {
vec[sort(rep(seq_along(vec), length.out = length))]
}
lengthen(a2, length(a1))
# [1] 1 1 1 3 3 3 4 4 5 5
lengthen(a3, length(a1))
# [1] 1 1 2 2 5 5 6 6 9 9
lengthen(a4, length(a1))
# [1] 5 5 5 1 1 1 3 3 4 4
@mrdwab
mrdwab / ASDT.R
Created June 15, 2020 06:45
`as.data.table` but adding in `names` from named `list` columns.
library(data.table)
## Sample data
L <- list(A = 1:2, B = NULL, C = 3:4)
DF <- data.frame(ID = 1:3, V1 = I(L), V2 = I(unname(L)), V3 = I(setNames(L, c("X", "Y", "Z"))))
DF2 <- data.frame(ID = 1:3, V1 = letters[1:3], V2 = letters[4:6], V3 = letters[7:9])
DF3 <- do.call(rbind, replicate(1000, DF, FALSE))
set.seed(1)
DF4 <- data.frame(ID = 1:300000, V1 = I(rep(L, 100000)), V2 = I(rep(unname(L), 100000)),
set.seed(1)
data_pos <- sample(0:50, 100, TRUE)
data_neg <- sample(-50:-1, 100, TRUE)
data_pos_neg <- c(0, sample(-50:50, 100, TRUE))
x <- runif(50, -5, 5)
grouped_stem <- function(invec, n = 2) {
if (!all(as.numeric(invec) == as.integer(invec))) stop("This function only works with integers")
invec <- sort(invec)
negative <- if (any(invec < 0)) TRUE else FALSE
library(data.table)
library(SOfun)
ragged <- function(indt, keys, blank = "") {
require(data.table)
indt <- setkeyv(as.data.table(indt), keys)
vals <- setdiff(names(indt), keys)
nams <- paste0(keys, "_copy")
for (i in seq_along(nams)) {
indt[, (nams[i]) := c(as.character(get(key(indt)[i])[1]),
myFun <- function(vec, find, replace) {
if (length(find) != length(replace)) stop("incompatible find/replace")
if (all(find %in% vec)) {
pos <- which(vec == find[1])
for (i in seq_along(pos)) {
ind <- pos[i]:(pos[i]+length(find)-1)
if (identical(vec[ind], find)) vec[ind] <- replace
}
} else {
message("nothing changed")
@mrdwab
mrdwab / subfix.r
Last active May 11, 2020 19:40
R script to fix manually edited srt files to upload to YouTube. Specifically, it converts the timestamps from SRT or SBV files to make sure the end and start times don't overlap.
#!/usr/local/bin/r
suppressMessages(library(docopt))
suppressMessages(library(glue))
options(useFancyQuotes = FALSE)
doc <- "Usage: subfix.r [FILE] [-h]
-h --help show this help text"
opt <- docopt(doc)