[R] Retaining attributes of columns of a data frame when subsetting.

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Mon Oct 21 00:53:56 CEST 2019


On 21/10/19 11:07 AM, Rui Barradas wrote:

> Hello,
> 
> Sorry, you're right, in the method it's x, X is the test dataframe.
> Repost:
> 
> `[.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
> }
> 
> 
> The (frequent) error comes from tests where a X was created in the 
> globalenv and found by the method.

Yep!  Happens to me all the time! :-)

Thanks very much.

cheers,

Rolf

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



More information about the R-help mailing list