You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
I() does not preserve a vector's names in data.table unlike data.frame #4793
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?
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 datadt= 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
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.
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).
Activity
jangorecki commentedon Nov 3, 2020
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.
dcaseykc commentedon Nov 3, 2020
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.jangorecki commentedon Nov 3, 2020
I think the following could do the job
or if needed to be very verbose
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.
But for performance it still seems better to produce 2 columns, one with a names and another with values.
MichaelChirico commentedon Jul 11, 2025
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).