I want to perform an operation on a subset of rows in a data.table
that result in a greater number of rows than what I started out with. Is there an easy way to expand the original data.table
to accommodate this? If not, how could I accomplish this?
Here's a sample of my original data.
DT <- data.table(my.id=c(1,2,3), unmodified=c("a","b","c"), vals=c("apple",NA,"cat"))
DT
my.id unmodified vals
1: 1 a apple
2: 2 b NA
3: 3 c cat
And this is my desired output.
DT
my.id unmodified vals
1: 1 a apple
2: 2 b boy
3: 2 b bat
4: 2 b bag
5: 3 c cat
The new rows can appear at the end as well, I don't care about the order. I tried DT[my.id == 2, vals := c("boy","bat","bag")]
, but it ignores the last 2 entries with a warning.
TIA!
EDIT: My original dataset has about 10 million rows, although the entry with a missing value occurs just once. I'd prefer not to create copies of the data.table
, if possible.