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
setDT modifies by reference, as do all the set* functions.
To turn something that is not a list into a data.table, you cannot simply modify its attributes by reference, e.g., your is.list(join_vector) # FALSE, so it's not clear what desired result you want. To create a data.table, use a different function. I don't want to have to wrap my head around "The set* functions all modify by reference, EXCEPT..."
I do expect set* to continue to update by reference for vectors; why is this impossible? Curious. Could be that I don't have a solid enough understanding of how by-reference updates work.
If you type setDT in the console, you can see how it works. All of the substantial steps use other set* functions or alloc.col (which also modifies by reference). I don't think you could add another block in here to modify a named vector by reference, but you could try. Alternately, you could just make a habit of working with lists... because why wouldn't you? (Just a rhetorical question.)
I was actually looking at an SO question today that's very similar to this - http://stackoverflow.com/q/30344192/817778. So you can put a vector inside a data.table without copying any data, which I think qualifies as "in-place" conversion.
@eantonya Very cool! However, that operation treats the vector like a column vector, not like a full data.table (creating one column per element of the vector).
@eantonya Oh, I thought your first comment was suggesting that you supported the proposal at hand (which is what I described: vector => column per element) because it's possible to modify vectors by reference. Anyway, that is a cool trick.
Neat. If I understand correctly, then, why not just update setDT's native behavior to (when encountering a vector) wrap it with list and get keep.rownames to work by assigning the same names? Expected result of setDT(join_vector, keep.rownames=T) being something like:
I took a stab at this today and the current version appears to make a copy (as evidenced by checking address before and after use of setDT). See my fork.
@jangorecki yes, I abandoned the recursive call when I realized it'd be much simpler to just imitate the list approach given the simplification that the length as a list is one.
@jangorecki I was thinking of that, but the keep.rownames option seems to be a natural (read: concise and parallel to usage for other types) fit for the case when we want to convert a named vector to a two-column data.table. Of course if there's no way to convert an atomic type by reference, perhaps we should just focus our energies on adding keep.rownames to as.data.table and/or data.table().
I would close that one. Vector is not compatible data type to be converted to data.table by reference, it is better leave atomic vector support or as.data.table.
Sure, makes sense, the main thing i was going for was a method to go from named vector to two-column data.table. By-reference part would just be a bonus & I was holding out hope for what Eddi mentioned as a possibility. For a single vector, the efficiency aspect should be really marginal.
Though, I happened to notice that apparently copy (and hence as.data.table) was really slow on e.g. 1B-length vector:
Activity
franknarf1 commentedon Jul 24, 2015
setDTmodifies by reference, as do all theset*functions.To turn something that is not a list into a data.table, you cannot simply modify its attributes by reference, e.g., your
is.list(join_vector) # FALSE, so it's not clear what desired result you want. To create a data.table, use a different function. I don't want to have to wrap my head around "Theset*functions all modify by reference, EXCEPT..."MichaelChirico commentedon Jul 24, 2015
I do expect
set*to continue to update by reference for vectors; why is this impossible? Curious. Could be that I don't have a solid enough understanding of how by-reference updates work.franknarf1 commentedon Jul 24, 2015
If you type
setDTin the console, you can see how it works. All of the substantial steps use otherset*functions oralloc.col(which also modifies by reference). I don't think you could add another block in here to modify a named vector by reference, but you could try. Alternately, you could just make a habit of working with lists... because why wouldn't you? (Just a rhetorical question.)eantonya commentedon Jul 24, 2015
I was actually looking at an SO question today that's very similar to this - http://stackoverflow.com/q/30344192/817778. So you can put a vector inside a
data.tablewithout copying any data, which I think qualifies as "in-place" conversion.franknarf1 commentedon Jul 24, 2015
@eantonya Very cool! However, that operation treats the vector like a column vector, not like a full data.table (creating one column per element of the vector).
eantonya commentedon Jul 24, 2015
@franknarf1 that's what both
data.table(your_vec)andas.data.table(your_vec)do, so why wouldsetDTdo smth else?franknarf1 commentedon Jul 24, 2015
@eantonya Oh, I thought your first comment was suggesting that you supported the proposal at hand (which is what I described: vector => column per element) because it's possible to modify vectors by reference. Anyway, that is a cool trick.
MichaelChirico commentedon Jul 25, 2015
Neat. If I understand correctly, then, why not just update
setDT's native behavior to (when encountering a vector) wrap it withlistand getkeep.rownamesto work by assigning the samenames? Expected result ofsetDT(join_vector, keep.rownames=T)being something like:eantonya commentedon Jul 26, 2015
@franknarf1 I don't think a row-vector is what OP is looking for - they're looking for a regular column vector, just with names added.
@MichaelChirico yep, I think it's doable, and seems reasonable to me.
franknarf1 commentedon Jul 26, 2015
Oh I see, my mistake. Sorry for the noise.
MichaelChirico commentedon Sep 18, 2015
@eantonya I'm not sure I understand your update to your previously referenced SO post. Does this mean we won't be able to do this so easily after all?
I took a stab at this today and the current version appears to make a copy (as evidenced by checking
addressbefore and after use ofsetDT). See my fork.jangorecki commentedon Sep 18, 2015
@MichaelChirico regarding your 41d4a77 commit.
I would avoid recursive call when it is not necessary and go with something like this:
but this is just my (not so much skilled) opinion.
Still I'm not sure if it is correct to make
setDTon atomic vector, might be better to put that optimization inas.data.tablemethod for atomic types?MichaelChirico commentedon Sep 18, 2015
@jangorecki yes, I abandoned the recursive call when I realized it'd be much simpler to just imitate the list approach given the simplification that the length as a list is one.
MichaelChirico commentedon Sep 19, 2015
@jangorecki I was thinking of that, but the
keep.rownamesoption seems to be a natural (read: concise and parallel to usage for other types) fit for the case when we want to convert a named vector to a two-columndata.table. Of course if there's no way to convert an atomic type by reference, perhaps we should just focus our energies on addingkeep.rownamestoas.data.tableand/ordata.table().jangorecki commentedon Mar 16, 2019
I would close that one. Vector is not compatible data type to be converted to data.table by reference, it is better leave atomic vector support or
as.data.table.MichaelChirico commentedon Apr 13, 2019
Sure, makes sense, the main thing i was going for was a method to go from named vector to two-column
data.table. By-reference part would just be a bonus & I was holding out hope for what Eddi mentioned as a possibility. For a single vector, the efficiency aspect should be really marginal.Though, I happened to notice that apparently
copy(and henceas.data.table) was really slow on e.g. 1B-length vector: