On 21/10/19 1:15 AM, Rui Barradas wrote:

Hello,

Richard's idea is good but shouldn't it be `[.myclass` instead?

Yes, I kind of thought that, and cobbled together something on that basis that seemed to work. However my code was rather a hodge-podge. I kept having to work around errors that I didn't understand.

You seem to have a much clearer understanding of what's going on, and your code is much cleaner than what I came up with.



`[.myclass` <- function(x, i, j, drop = if (missing(i)) TRUE else length(cols) == 1){
   SaveAt <- lapply(X, attributes)
   X <- NextMethod()
   lX <- lapply(names(X),function(nm, x, Sat){
     attributes(x[[nm]]) <- Sat[[nm]]
     x[[nm]]}, x = X, Sat = SaveAt)
   names(lX) <- names(X)
   X <- as.data.frame(lX)
   X
}

But in the foregoing there seems to me to be some inconsistency in the use of "X" and "x".

Should not the first function argument be "X" rather than "x"? Or perhaps the "X" symbols in the code should be replaced by "x"? As in:

    SaveAt <- lapply(x, attributes)
    x <- NextMethod()
    ....
    ....

Or am I misunderstanding what's going on (as is so often
the case! :-( )?

X <- data.frame(a = letters[1:5], x = 1:5)
class(X) <- c("myclass", class(X))
attr(X$a, "attr_a1") <- "first_a"
attr(X$a, "attr_a2") <- "second_a"
str(X)

ok <- c(1, 3, 4)
X <- X[ok, ]
str(X)


Hope this helps,

Quite a lot! But I'd appreciate clarification w.r.t. the misgiving that I expressed above.

Thanks.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to