(I\'m using the word \"field\" in the mathematical sense; base fields/corpora which R
already uses include the real and complex numbers.)
I\'m intereste
I found Hadley Wickham's devtools wiki an invaluable resource for getting started with classes in R. In particular, read the sections on:
Here is a starting point that illustrates some of the concepts in S3
classes. Let's call your new class f5
. At a minimum, you would probably want to create methods for:
as.f5
is.f5
+.f5
print.f5
Some code (using digitsBase
in package GLDEX
to do the base conversion):
library(GLDEX)
as.f5 <- function(x){
if(!inherits(x, "f5")) class(x) <- c("f5", class(x))
x
}
is.f5 <- function(x){
inherits(x, "f5")
}
`+.f5` <- function(e1, e2){
NextMethod(e1, e2)
}
print.f5 <- function(x, ...){
# Next line from ?GLDEX::digitsBase
b2ch <- function(db) noquote(gsub("^0+(.{1,})$"," \1",
apply(db, 2, paste, collapse = "")))
cat("Base 5:\n")
cat(b2ch(digitsBase(x, 5)))
invisible(x)
}
x <- as.f5(0:10)
y <- as.f5(5)
x + y
Base 5:
10 11 12 13 14 20 21 22 23 24 30