Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205301

Filling a data.table from smaller data.tables

$
0
0

I am looking for a way to fill a Result data.table from smaller data.tables that come from calculations. My approach was the following:

#CREATE EXAMPLE

library(data.table)

# The empty table to be filled

DT <- data.table(
   "ID" = c("a", "b", "c", "d"),
   "A" = numeric(4),
   "B" = numeric(4))

   ID A B
1:  a 0 0
2:  b 0 0
3:  c 0 0
4:  d 0 0

# Table with part of the results
DT_short <- data.table(
         "ID" = c("a", "b", "d"),
         "A" = 1:3,
         "B" = 1:3)

   ID A B
1:  a 1 1
2:  b 2 2
3:  d 3 3

What I would like to do is to fill rows and columns according to their name. I managed to access the part of the big data.table I want to change by

nm1 <- names(DT_short)
DT[ID %in% DT_short[, ID], ..nm1]
#Bonus question: Why do I have to assign nm1 before, how do I make it work directly in []?

Now I would like to replace this part of DT by the small table DT_short, but everything I tried (like <- or :=, or some kind of merge) didn't work. E.g. error object '..nm1' not found for DT[ID %in% DT_short[, ID], ..nm1] <- DT_short

Please help me by providing a solution or pointing me in the right direction. (Since the data I am working with is rather small - 10^2 columns, 10^2 rows, ~40 small files to be combined, number<10^9 per field - and other people will use my code readability is more important than performance.)


Viewing all articles
Browse latest Browse all 205301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>