I am very new to S3 and was wondering if it was possible given the following data frame:
test <- tibble(
elements = c("one", "two", "three"),
S3 = c("foo", "bar", "foo")
)
I could give each element in the elements
column a custom class from the S3 column:
custom_class <- function(x, customclass) {
class(x) <- c(class(x), customclass)
return(x)
}
# test
s <- "string"
custom_class(s, "anything")
test <- tibble(
elements = c("one", "two", "three"),
S3 = c("foo", "bar", "foo"),
testing = custom_class(elements, S3)
)
But this doesn't work. Is this due to a mental model gap in my understanding of S3? Is it possible to apply different classes to each element in this way, and if not maybe create the output as a list in some way so that each element in the list is an element
with class S3
? Any tips/help advice appreciated!