rdataframeattributesdplyrtibble

Why does a given attribute disappears after using dplyr::mutate?


I have given some attributes to my data frame.

This just saves me some typing. I work a lot with the dplyr package and especially with the mutate command.

But after using mutate on my data frame the attributes I have given to the data frame disappear.

Does anyone know why R or dplyr is doing that?

Here is a small example:

df <- data.frame(n = seq(1,1000),
       abc = rep(1,1000))
library(dplyr); library(data.table)
df <- df %>% setattr(., "my_attribute", "this thing is 1000 entries long") %>% 
                mutate_at(.vars = "abc", as.character)

... and if I list my attributes, R gives me:

> str(attributes(df))
List of 3
$ class    : chr "data.frame"
$ names    : chr [1:2] "n" "abc"
$ row.names: int [1:1000] 1 2 3 4 5 6 7 8 9 10 ...

Solution

  • Note: I just got a checkmark on this but when I run it on a current version of R and data.table, I do not see the loss of the attribute. I'm wondering if that infelicity has been addressed by the package maintainer?

    The mutate function is resulting in the expected loss of attributes (even though you only coerced a single column to a different class.) So set your attribute after the mutate-operation:

    df <- df %>% mutate_at(.vars = "abc", as.character) %>% 
                      setattr(.data, "my_attribute", "this thing is 1000 entries long")
    #> names(attributes(df))
    #[1] "class"        "names"        "row.names"    "my_attribute"
    

    -----------base R equivalent----

    df <- df %>% mutate_at(.vars = "abc", as.character)  
    attr( df, "my_attribute") <- "this thing is 1000 entries long"