[datatable-help] Better hacks?: getting a vector AND using 'with'; inserting chunks of rows

David Kulp dkulp at dizz.org
Fri May 10 03:05:43 CEST 2013


dt[row.num,][[col.name]] is indeed the solution.  And works of course for data.frames, too.  Maybe it should be added to the FAQ #1.3.
Thank you!

On May 8, 2013, at 4:00 PM, Frank Erickson <FErickson at psu.edu> wrote:

> For your first question, this should work:
> 
> dt[row.num,][[col.name]]
> 
> For the second question, I guess your problem goes away if you aren't using an (all but) NULL data.table.
> 
> dt <- data.table(x=1,y=1)
> nr <- data.table(NA,NA)
> 
> rbind(dt,nr,use.names=FALSE)
> #    x  y
> #1:  1  1
> #2: NA NA
> 
> So, if you're dynamically growing your data.table from nothing, you'll only have to assign the colnames once, after the data.table becomes non-empty. I've read that R is pretty inefficient at dynamically growing things, ...as you say, it's a copy operation, right?
> 
> I hope this helps.
> 
> Best,
> 
> Frank
> 
> 
> On Wed, May 8, 2013 at 11:31 AM, David Kulp <dkulp at dizz.org> wrote:
> I must be doing something stupid.  I'd like to get a vector from a data.frame column using with=FALSE instead of a single-column data.table.
> 
> dt <- data.table(x=1:10,y=letters[1:10])
> col.name <- 'y'
> row.num <- 5
> print(dt[row.num,y])  # returns a vector with the letter 'e'.  OK.
> print(dt[row.num,list(y)])  # returns a data.table.  OK.
> print(dt[row.num, col.name ,with=FALSE])  # returns a data.table... no list syntax here but I don't get a vector back.  Not OK.
> 
> The best I can do is
> 
> unlist(as.list(dt[row.num, col.name ,with=FALSE]))
> 
> which seems rather hackish.
> 
> I've read the FAQ and I'm stymied.  v1.8.8.  Any help?
> 
> ----
> 
> While I've got your attention, I might as well ask another stupid question.  I can't insert new rows automagically.
> 
> dt[11] <- c(11,'k')
> 
> Although I can do
> 
> df <- as.data.frame(dt)
> df[11,] <- c(11,'k')
> 
> So I figure you want me to use rbind, even though rbind.data.table is probably a copy operation.
> 
> dt <- rbind(dt, list(x=11,y='k'))
> 
> But I'd like to start with an empty data.table and programmatically add chunks of rows as I run out of space.  So I generate a data.table of NA values and rbind.  E.g., here I want to add 5 new rows to the 2 column table.
> 
> dt <- data.table(x=numeric(), y=character())
> new.rows <- lapply(1:2, function(c) { rep(NA, 5) })
> 
> dt <- rbind(dt, new.rows, use.names=FALSE)
> 
> According to the documentation, rbind is supposed to copy by position if use.names=FALSE, but it doesn't retain the column names.  This worked in v1.8.2.  Then I upgraded and it stopped working.  I know I can fix this by labeling the columns of new.rows, but I'm guessing that there's a much better way to simply allocate a new chunk of rows to a growing table and I didn't see any info online.
> 
> Thanks in advance!!
> 
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130509/c4c0c625/attachment.html>


More information about the datatable-help mailing list