R - Join on data.table, selecting a different column based on value of another column in row

hgcrpd

I have two data.tables:

DT1 <- data.table(A=c('A', 'B', 'C'), idx=c(1,2,3))
DT2 <- data.table(idx=c(1,2,3), A=rep('foo', 3), B=rep('bar', 3), C=rep('baz', 3))

> DT1
   A idx
1: A   1
2: B   2
3: C   3

> DT2
   idx   A   B   C
1:   1 foo bar baz
2:   2 foo bar baz
3:   3 foo bar baz

And I want to get to this:

> DT3
   idx value
1:   1   foo
2:   2   bar
3:   3   baz

Basically I want to merge DT1 and DT2 on idx, but I only want the column from DT2 that corresponds to the value of A in the row of DT1. both DT1 and DT2 have the same number of rows and are in the rows are in same order

Is there a data.table native way to do this?

Arun

Updated answer following the old (implicit) by-without-by feature being replaced with by=.EACHI (and also using on= argument instead of setting key:

require(data.table) # v1.9.6+
DT2[DT1, .(value=get(i.A)), on="idx", by=.EACHI]
#    idx value
# 1:   1   foo
# 2:   2   bar
# 3:   3   baz

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting a column name dynamically (based on another table row's value)

From Dev

R- selecting a row based on characteristics of another column in that row

From Dev

selecting a column based on a minimum value of another column

From Dev

Selecting row with highest ID based on another column

From Dev

Set the default value of a column based on another column of a different data type

From Dev

Selecting value based on variable name of data frame column in R

From Dev

Selecting value based on variable name of data frame column in R

From Dev

R: selecting a different row at every column efficiently

From Dev

R: selecting a different row at every column efficiently

From Dev

R Data.table divide values in column based on another column

From Dev

selecting a column value based on a different column value after applying Groupby

From Dev

how to fetch data with same id in a row but different value in another column and display into table in php

From Dev

Mysql Query - Get row value based on column name in a different table

From Dev

How do we pass different value to another activity by selecting choice-based table row in a table

From Dev

R data table: Assign a value to column based on reference column

From Dev

Selecting data from table where sum of values in a column equal to the value in another column

From Dev

R- How to move value in a specific column to a different row based on a value in a different column

From Dev

Return row of Data Frame based on value in a column - R

From Dev

Return row of Data Frame based on value in a column. R script

From Dev

R - How to get value from a column based on value from another column of same row

From Dev

Inner Join table based on column value

From Dev

Duplicate row based on value in different column

From Dev

Add column to data frame based on values of another column in another row

From Dev

SQL - Selecting a column from another table twice with different values

From Dev

Merge rows based on column value and value different column in next row

From Dev

Pandas: Selecting value from preceding row and different column

From Dev

Selecting specific row based on column value and inserting a formula

From Dev

mysql - Select unique column based on max value of another column in a different table

From Dev

R - fill rows of a column down based on another row in another column

Related Related

  1. 1

    Selecting a column name dynamically (based on another table row's value)

  2. 2

    R- selecting a row based on characteristics of another column in that row

  3. 3

    selecting a column based on a minimum value of another column

  4. 4

    Selecting row with highest ID based on another column

  5. 5

    Set the default value of a column based on another column of a different data type

  6. 6

    Selecting value based on variable name of data frame column in R

  7. 7

    Selecting value based on variable name of data frame column in R

  8. 8

    R: selecting a different row at every column efficiently

  9. 9

    R: selecting a different row at every column efficiently

  10. 10

    R Data.table divide values in column based on another column

  11. 11

    selecting a column value based on a different column value after applying Groupby

  12. 12

    how to fetch data with same id in a row but different value in another column and display into table in php

  13. 13

    Mysql Query - Get row value based on column name in a different table

  14. 14

    How do we pass different value to another activity by selecting choice-based table row in a table

  15. 15

    R data table: Assign a value to column based on reference column

  16. 16

    Selecting data from table where sum of values in a column equal to the value in another column

  17. 17

    R- How to move value in a specific column to a different row based on a value in a different column

  18. 18

    Return row of Data Frame based on value in a column - R

  19. 19

    Return row of Data Frame based on value in a column. R script

  20. 20

    R - How to get value from a column based on value from another column of same row

  21. 21

    Inner Join table based on column value

  22. 22

    Duplicate row based on value in different column

  23. 23

    Add column to data frame based on values of another column in another row

  24. 24

    SQL - Selecting a column from another table twice with different values

  25. 25

    Merge rows based on column value and value different column in next row

  26. 26

    Pandas: Selecting value from preceding row and different column

  27. 27

    Selecting specific row based on column value and inserting a formula

  28. 28

    mysql - Select unique column based on max value of another column in a different table

  29. 29

    R - fill rows of a column down based on another row in another column

HotTag

Archive