Skip to content

Instantly share code, notes, and snippets.

View mrdwab's full-sized avatar

Ananda Mahto mrdwab

View GitHub Profile
upper_left <- function(n, diag = TRUE, byrow = FALSE) {
x <- seq.int(n)
tmp1 <- sequence(rev(x))
tmp2 <- rep(x, rev(x))
out <- if (byrow) {
cbind(row = tmp2, col = tmp1)
} else {
cbind(row = tmp1, col = tmp2)
}
if (diag) out else out[rowSums(out) != n + 1, ]
@mrdwab
mrdwab / advent_1.R
Last active December 18, 2020 22:09
fun_for <- function(x, target, n) {
if (!n %in% c(2, 3)) stop("The accounting Elves are crazy!")
if (n == 2) {
out <- x[(target - x) %in% x]
} else if (n == 3) {
out <- numeric(0)
for (i in seq_along(x)) {
s1 <- x + x[i]
for (j in seq_along(s1)) {
s2 <- s1 + x[j]
# Using `fread` and `fwrite` to paste together columns like `do.call(paste, ...)`
fpaste <- function(dt, sep = ",") {
x <- tempfile()
if (sep == "") {
data.table(V1 = do.call(stringi::stri_join, c(dt, sep = "")))
} else {
fwrite(dt, file = x, sep = sep, col.names = FALSE)
fread(x, sep = "\n", header = FALSE)
}
}
@mrdwab
mrdwab / 65227663.R
Last active December 10, 2020 05:27
Testing different approaches for replacing characters with integers. https://stackoverflow.com/q/65227663/1270695
## If your replacement is just a sequence of integers the length of the unique values being factored,
## you can create a function like this which should be quite fast.
fac2int <- function(x, levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA) {
as.integer(factor(x, levels, labels, exclude, ordered, nmax))
}
### DIFFERENT APPROACHES TO TEST
fun_datamatrix <- function() {
df[] <- data.matrix(as.data.frame(lapply(df, factor, levels = df2$Group)))
@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)),