简体   繁体   中英

In `data.table` in R, is there a way to fast-assign values to rows based on an index?

I am currently working with a data.table table that has approximately 200 million rows.

>table
user     age
A        19
B        22
C        18
D        13
E        93
F        15
G        11
H        16
I        33
J        25
K        44
L        23
M        76
N        34
O        18
P        32
Q        55

Additionally, I have an "index" table that looks like:

> index
row_number     count
1              5
3              7
7              12
8              100
12             3
14             4

My goal is to be able to append the count column into table . The row_number column represents the rows numbers of table . Hence, when row_number equals 1, we append the value 5 to the user A and age 19 row of table . For row_number equals 3, we insert the value 7 to user C and age 18. There are gaps between, so I would like to fill them with 0.

So overall, I'd like:

>table
user     age    count 
A        19     5
B        22     0
C        18     7
D        13     0
E        93     0
F        15     0
G        11     12
H        16     100
I        33     0
J        25     0
K        44     0
L        23     3
M        76     0
N        34     4
O        18     0
P        32     0
Q        55     0

so far, my code for doing this is:

table[,count:= count, by=.N]

However, I am not able to get a correct sort. Does anyone know how I can accomplish this in data.table ? Thank you!

Here is an approach using set

# set everything to 0
set(table, j = 'count', value = 0)
# replace the appropriate indices with the relevant values
set(table, j = 'count', i = index[['rownumber']], j = index[['count']])

You can also use the := operator. You don't need by here. Instead you could do it as:

table[, count := 0L][index$row_number, count := index$count]

First we initialise count with integer value 0 , and then for the row numbers given in i , we modify count of table in-place with the corresponding count values from index .

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM