On 5/11/19 3:41 AM, Hadley Wickham wrote:> For what it's worth, I don't think this strategy can work in general, > because a class might have attributes that depend on its data/contents > (e.g. https://vctrs.r-lib.org/articles/s3-vector.html#cached-sum). I > don't think these are particularly common in practice, but it's > dangerous to assume that you can restore a class simply by restoring > its attributes after subsetting.You're probably right that there are lurking perils in general, but I am not trying to "restore a class". I simply want to *retain* attributes of columns in a data frame. * I have a data frame X * I attach attributes to certain of its columns; attr(X$melvin,"clyde") <- 42 (I *don't* change the class of X$melvin.) * I form a subset of X: Y <- X[1:100,3:10] * given that "melvin" is amongst columns 3 through 10 of X, I want Y$melvin to retain the attribute "clyde", i.e. I want attr(Y$melvin,"clyde") to return 42 There is almost surely a better approach than the one that I've chosen (isn't there always?) but it seems to work, and the perils certainly are not immediately apparent to me. cheers, Rolf -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Hi Rolf, On 11/4/19 12:28, Rolf Turner wrote:> > On 5/11/19 3:41 AM, Hadley Wickham wrote: > >> For what it's worth, I don't think this strategy can work in general, >> because a class might have attributes that depend on its data/contents >> (e.g. >> https://urldefense.proofpoint.com/v2/url?u=https-3A__vctrs.r-2Dlib.org_articles_s3-2Dvector.html-23cached-2Dsum&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=pqLHzHYLUeyQnxA1K_XhSbKJql6r9wK1RXcDG2tuZ6s&s=kPUlNqBPr6j4lPvqkIj8w2Gl5JYGLqJ7ws6wH5tpGcw&e= >> ). I >> don't think these are particularly common in practice, but it's >> dangerous to assume that you can restore a class simply by restoring >> its attributes after subsetting. > > > You're probably right that there are lurking perils in general, but I am > not trying to "restore a class".? I simply want to *retain* attributes > of columns in a data frame. > > * I have a data frame X > * I attach attributes to certain of its columns; > ???? attr(X$melvin,"clyde") <- 42 > ? (I *don't* change the class of X$melvin.) > * I form a subset of X: > ??? Y <- X[1:100,3:10] > * given that "melvin" is amongst columns 3 through 10 of X, > ??? I want Y$melvin to retain the attribute "clyde", i.e. I > ??? want attr(Y$melvin,"clyde") to return 42 > > There is almost surely a better approach than the one that I've chosen > (isn't there always?) but it seems to work, and the perils certainly are > not immediately apparent to me.Maybe you've solved the problem for the columns that contain your objects but now you've introduced a potential problem for columns that contain objects with attributes whose value depend on content. Hadley it right that restoring the original attributes of a vector (list or atomic) after subsetting is unsafe. Best, H.> > cheers, > > Rolf >-- Herv? Pag?s Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpages at fredhutch.org Phone: (206) 667-5791 Fax: (206) 667-1319
> the perils certainly are not immediately apparent to me.Here is a concrete example of a peril `[.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 } x <- data.frame(Mat=I(matrix(101:106,ncol=2)), Vec=201:203) xmc <- structure(x, class=c("myclass", class(x))) xmc[1:2,] Error in attributes(x[[nm]]) <- Sat[[nm]] : dims [product 6] do not match the length of object [4] x[1:2,] Mat.1 Mat.2 Vec 1 101 104 201 2 102 105 202 I would be surprised if extracting a column from some rows of a data.frame gave a different result than extracting some rows from a column of a data.frame. The row-selecting method used by [.data.frame depends on the class of the column. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Nov 4, 2019 at 12:28 PM Rolf Turner <r.turner at auckland.ac.nz> wrote:> > On 5/11/19 3:41 AM, Hadley Wickham wrote: > > > For what it's worth, I don't think this strategy can work in general, > > because a class might have attributes that depend on its data/contents > > (e.g. https://vctrs.r-lib.org/articles/s3-vector.html#cached-sum). I > > don't think these are particularly common in practice, but it's > > dangerous to assume that you can restore a class simply by restoring > > its attributes after subsetting. > > > You're probably right that there are lurking perils in general, but I am > not trying to "restore a class". I simply want to *retain* attributes > of columns in a data frame. > > * I have a data frame X > * I attach attributes to certain of its columns; > attr(X$melvin,"clyde") <- 42 > (I *don't* change the class of X$melvin.) > * I form a subset of X: > Y <- X[1:100,3:10] > * given that "melvin" is amongst columns 3 through 10 of X, > I want Y$melvin to retain the attribute "clyde", i.e. I > want attr(Y$melvin,"clyde") to return 42 > > There is almost surely a better approach than the one that I've chosen > (isn't there always?) but it seems to work, and the perils certainly are > not immediately apparent to me. > > cheers, > > Rolf > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
On 04/11/2019 4:40 p.m., Pages, Herve wrote:> Hi Rolf, > > On 11/4/19 12:28, Rolf Turner wrote: >> >> On 5/11/19 3:41 AM, Hadley Wickham wrote: >> >>> For what it's worth, I don't think this strategy can work in general, >>> because a class might have attributes that depend on its data/contents >>> (e.g. >>> https://urldefense.proofpoint.com/v2/url?u=https-3A__vctrs.r-2Dlib.org_articles_s3-2Dvector.html-23cached-2Dsum&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=pqLHzHYLUeyQnxA1K_XhSbKJql6r9wK1RXcDG2tuZ6s&s=kPUlNqBPr6j4lPvqkIj8w2Gl5JYGLqJ7ws6wH5tpGcw&e>>> ). I >>> don't think these are particularly common in practice, but it's >>> dangerous to assume that you can restore a class simply by restoring >>> its attributes after subsetting. >> >> >> You're probably right that there are lurking perils in general, but I am >> not trying to "restore a class".? I simply want to *retain* attributes >> of columns in a data frame. >> >> * I have a data frame X >> * I attach attributes to certain of its columns; >> ???? attr(X$melvin,"clyde") <- 42 >> ? (I *don't* change the class of X$melvin.) >> * I form a subset of X: >> ??? Y <- X[1:100,3:10] >> * given that "melvin" is amongst columns 3 through 10 of X, >> ??? I want Y$melvin to retain the attribute "clyde", i.e. I >> ??? want attr(Y$melvin,"clyde") to return 42 >> >> There is almost surely a better approach than the one that I've chosen >> (isn't there always?) but it seems to work, and the perils certainly are >> not immediately apparent to me. > > Maybe you've solved the problem for the columns that contain your > objects but now you've introduced a potential problem for columns that > contain objects with attributes whose value depend on content. > > Hadley it right that restoring the original attributes of a vector (list > or atomic) after subsetting is unsafe.Right, so Rolf should only restore attributes that are ones he added in the first place. Unknown attributes should be left alone. Duncan Murdoch