Last active
August 21, 2018 09:44
-
-
Save AkselA/f076e8402fc5b4a395bb2196cd698c60 to your computer and use it in GitHub Desktop.
Example code for https://aksela.wordpress.com/2018/08/18/simple-example-for-creating-a-custom-s3-class-with-methods/
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
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." |
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
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) |
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
class(sampletext) <- c("text", "character") | |
class(sampletext) | |
#[1] "text" "character" |
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
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. |
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
statmode <- function(...) { | |
UseMethod("statmode") | |
} |
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
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)] | |
} |
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
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