Skip to content

Instantly share code, notes, and snippets.

@AkselA
Last active August 21, 2018 09:44
Show Gist options
  • Save AkselA/f076e8402fc5b4a395bb2196cd698c60 to your computer and use it in GitHub Desktop.
Save AkselA/f076e8402fc5b4a395bb2196cd698c60 to your computer and use it in GitHub Desktop.
sampletext <- "Technically, five-bit codes began in the 17th century, when Francis Bacon developed the cipher now called Bacon's cipher. The cipher was not designed for machine telecommunications (it was instead a method of encrypting a hidden message into another) and, although in theory it could be adapted to that purpose, it only covered 24 of the 26 letters of the English alphabet (two sets of letters, I/J and U/V, were expressed with the same code) and contained no punctuation, spaces, numbers or control characters, rendering it of little use."
sampletext
#[1] "Technically, five-bit codes began in the 17th century, when Francis Bacon developed the cipher now called Bacon's cipher. The cipher was not designed for machine telecommunications (it was instead a method of encrypting a hidden message into another) and, although in theory it could be adapted to that purpose, it only covered 24 of the 26 letters of the English alphabet (two sets of letters, I/J and U/V, were expressed with the same code) and contained no punctuation, spaces, numbers or control characters, rendering it of little use."
cl.pr <- sub("print.", "", c(methods(print)))
cl.pl <- sub("plot.", "", c(methods(plot)))
cl.su <- sub("summary.", "", c(methods(summary)))
cl.un <- Reduce(union, list(cl.pr, cl.pl, cl.su))
sort(cl.un)
class(sampletext) <- c("text", "character")
class(sampletext)
#[1] "text" "character"
print.text <- function(x, width=80) {
x <- strwrap(x, width)
cat(x, sep="\n")
}
sampletext
# Technically, five-bit codes began in the 17th century, when Francis Bacon
# developed the cipher now called Bacon's cipher. The cipher was not designed for
# machine telecommunications (it was instead a method of encrypting a hidden
# message into another) and, although in theory it could be adapted to that
# purpose, it only covered 24 of the 26 letters of the English alphabet (two sets
# of letters, I/J and U/V, were expressed with the same code) and contained no
# punctuation, spaces, numbers or control characters, rendering it of little use.
statmode <- function(...) {
UseMethod("statmode")
}
statmode.text <- function(x) {
lett <- tolower(gsub("[^a-zA-z]", "", x))
fact <- factor(strsplit(lett, "")[[1]])
tabl <- table(fact)
tabl[which.max(tabl)]
}
statmode.character <- function(x) {
word <- tolower(sub("'.*", "", x))
tabl <- table(word)
tabl[which.max(tabl)]
}
statmode.numeric <- function(x, na.rm=FALSE) {
if (na.rm) {
x <- x[!is.na(x)]
}
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
statmode.default <- function(x) {
tabl <- table(x)
tabl[which.max(tabl)]
}
sampletext.char <- unlist(strsplit(gsub("[,.()]", "", sampletext), " "))
sample.num <- c(1, 3, 2, 7, 9, 0, 5, 3, 1, 6, 7, 5, 2, 4, 7, 4, 0, 7, 1, 8)
sample.fac <- factor(sample.num)
statmode(sampletext)
# e
# 59
statmode(sampletext.char)
# the
# 6
statmode(sample.num)
# [1] 7
statmode(sample.fac)
# 7
# 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment