Last active
October 13, 2015 20:17
-
-
Save mrdwab/4249862 to your computer and use it in GitHub Desktop.
Sampling with string as a seed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## @knitr tdasample | |
TDASample <- function(inString, N, n, toFile = FALSE) { | |
if (is.factor(inString)) inString <- as.character(inString) | |
if (nchar(inString) <= 3) stop("inString must be > 3 characters") | |
string1 <- "jnt3g127rbfeqixkos 586d90pyal4chzmvwu" | |
string2 <- "2dyn0uxq ovalrpksieb3fhjw584cm9t7z16g" | |
instring <- chartr(string1, string2, tolower(inString)) | |
t1 <- sd(c(suppressWarnings(sapply(strsplit(instring, ""), | |
as.numeric))), na.rm = TRUE) | |
t2 <- c(sapply(strsplit(instring, " "), nchar)) | |
t3 <- c(na.omit(sapply(strsplit(instring, ""), match, letters))) | |
seed <- floor(sum(t1, sd(t2), mean(t2), prod(fivenum(t3)), | |
mean(t3), sd(t3), na.rm=TRUE)) | |
set.seed(seed) | |
temp0 <- sample(N, n) | |
temp1 <- list( | |
Metadata = | |
noquote(c(sprintf(" The sample was drawn on: %s.", | |
Sys.time()), | |
sprintf(" The seed input was: '%s'", | |
inString), | |
sprintf("The total number of households was: %d.", N), | |
sprintf(" The desired number of samples was: %d.", n))), | |
SeedUsed = seed, | |
FinalSample = temp0, | |
FinalSample_sorted = sort(temp0)) | |
rm(.Random.seed, envir=globalenv()) | |
if (isTRUE(toFile)) { | |
capture.output(temp1, | |
file = paste("Sample from", | |
Sys.Date(), ".txt", | |
collapse=""), | |
append = TRUE) | |
} | |
temp1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment