Skip to content

I() does not preserve a vector's names in data.table unlike data.frame #4793

Closed

Description

@dcaseykc

According to the documentation of data.frame, I() can be used to prevent data.frame from stripping the names of a vector. By contrast, data.table strips the names still but does preserve the "asis" class. Is this difference expected?

library('data.table')
fake = setNames(1:3, c('a','b','c'))

data.frame(v1 = I(fake))[, 'v1']
#> a b c 
#> 1 2 3
class(data.frame(v1 = I(fake))[, 'v1'])
#> [1] "AsIs"
data.table(v1 = I(fake))[, v1]
#> [1] 1 2 3
class(data.table(v1 = I(fake))[, v1])
#> [1] "AsIs"

sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18363)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] data.table_1.13.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_4.0.2  magrittr_1.5    tools_4.0.2     htmltools_0.5.0
#>  [5] yaml_2.2.1      stringi_1.4.6   rmarkdown_2.3   highr_0.8      
#>  [9] knitr_1.29      stringr_1.4.0   xfun_0.16       digest_0.6.25  
#> [13] rlang_0.4.7     evaluate_0.14

Created on 2020-11-02 by the reprex package (v0.3.0)

Activity

  1. jangorecki commented on Nov 3, 2020

    @jangorecki
    Member

    I never seen use of named vector in data.table. I would suggest to put them in another column.
    If they should work is of course a valid question.

  2. dcaseykc commented on Nov 3, 2020

    @dcaseykc
    Author

    In case it helps, my use case is around computing the relative fractions of factors and I was hoping to store the factor levels in names. Here I've named the function relative_fraction, but the main approach would be to have some general function (e.g. calc) and use s3 dispatching to handle different classes. Because I'd like to use the same entry point for different types of variables, returning two columns sometimes and one column other times (particularly when J includes ./list) seems like it'll get tricky.

    library('data.table')
    
    #fake data
    dt = data.table(factor(sample(1:3, 100, replace = T), 1:3, c('a','b','c')))
    
    relative_fraction = function(x){
      r = as.numeric(table(x)/length(x))
      names(r) = levels(x)
      setattr(r, 'lvls', levels(x))
      
      return(r)
    }
    
    dt[, relative_fraction(V1)] #names and attribute are preserved
    #>    a    b    c 
    #> 0.26 0.32 0.42 
    #> attr(,"lvls")
    #> [1] "a" "b" "c"
    r1 = dt[, .(relative_fraction(V1))]
    r1[, V1] #only the attribute is preserved
    #> [1] 0.26 0.32 0.42
    #> attr(,"lvls")
    #> [1] "a" "b" "c"
    
    
    dt2 = data.table(factor(sample(1:3, 100, replace = T), 1:3, c('d','e','f')))
    dt2[, relative_fraction(V1)] #names and attributes
    #>    d    e    f 
    #> 0.42 0.29 0.29 
    #> attr(,"lvls")
    #> [1] "d" "e" "f"
    r2 = dt2[, .(relative_fraction(V1))]
    r2[, V1] #only the attributes
    #> [1] 0.42 0.29 0.29
    #> attr(,"lvls")
    #> [1] "d" "e" "f"
    
    #Ideally these would be the same
    c(dt[, relative_fraction(V1)], dt2[, relative_fraction(V1)]) #names, no attribute
    #>    a    b    c    d    e    f 
    #> 0.26 0.32 0.42 0.42 0.29 0.29
    c(r1[,V1], r2[,V1]) #not names and also not attribute
    #> [1] 0.26 0.32 0.42 0.42 0.29 0.29
    rbind(r1, r2)[,V1] #only the attributes of the first one (not ideal, but I think thats just how rbind works)
    #> [1] 0.26 0.32 0.42 0.42 0.29 0.29
    #> attr(,"lvls")
    #> [1] "a" "b" "c"
    
    
    rbind(data.frame(v = I(setNames(1, 'a'))), data.frame(v = I(setNames(1, 'b'))))[, 'v'] #names make it through rbind for data.frames
    #> a b 
    #> 1 1
  3. jangorecki commented on Nov 3, 2020

    @jangorecki
    Member

    I think the following could do the job

    dt[, relative_fraction(setNames(V1, V2))]

    or if needed to be very verbose

    dt[, {
      named_vec = setNames(V1, V2)
      relative_fraction(named_vec)
    }]

    wait a moment, now I see you want to return named vector rather than take it as input.
    I would suggest then to store them in a list, because inside a list you can nest whatever you want, including named vector.

    data.table(id=1:2, v=list(c("a"=1L,"b"=2L), c("z"=9L)))$v

    But for performance it still seems better to produce 2 columns, one with a names and another with values.

  4. MichaelChirico commented on Jul 11, 2025

    @MichaelChirico
    Member

    I agree we shouldn't really support named vectors in data.tables. It would be very complicated to keep track of that during e.g. grouping, what should happen when we group by c(a=1, b=1), should there be 1 or two groups? If one group, which name should be kept?

    Now with #7136, we can conveniently extract the names to a column during conversion: as.data.table(fake, keep.rownames=TRUE).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions