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

David Kulp dkulp at dizz.org
Wed May 8 18:31:39 CEST 2013


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!!



More information about the datatable-help mailing list