Get value in column from matrix of row numbers

Repmat

Here is the data setup:

require(data.table)
set.seed(42)
pos_mat <- data.table(c1 = sample(1:1000), c2 =  sample(1:1000), c3 = sample(1:1000))
data    <- data.table(value = rnorm(1000), other_stuff = rnorm(1000))

The table looks like this:

> pos_mat
    c1  c2  V3
1: 915 849 990
2: 937  63 439
3: 286 819 699
4: 828 538 887
5: 640 498 831

996: 118 793 783
997: 777 670 617
998: 579 195 643
999: 351 728 221
1000: 834 742 244

and:

> data
           value  other_stuff
   1: -0.6013830  0.617336710
   2: -0.1358161 -0.004541141
   3: -0.9872728 -0.091256360
   4:  0.8319250  0.399959375
   5: -0.7950595  0.588901657

 996: -0.3757455  0.264323016
 997: -1.0417354 -1.355822276
 998:  0.6976674  0.359071548
 999: -0.1444488 -1.708252839
1000:  0.4985434 -0.635928277

Now each element in pos_mat responds to a row number in data. I would a new data.table that has the same dimensions as pos_mat, but instead of having row numbers it hold the corrosponding value from data.

I.e. pos_mat[1,.(c1)] has the value 915. In data[915,.(value)] = 0.1702369 and I would like this to stored in the new object.

I feel that something like:

new <- pos_mat
n <- nrow(pos_mat)
for(i in n) new[i,] <- data[unlist(pos_mat[1,]), value]

Should work, but it keeps telling me the dimensions are wrong.

David Arenburg

Using a smaller data set

require(data.table)
set.seed(42)
pos_mat <- data.table(c1 = sample(1:10), c2 =  sample(1:10), c3 = sample(1:10))
data <- data.table(value = rnorm(10), other_stuff = rnorm(10))

If you want a data.table solution, you could use set and update pos_dat (or any other data set) in place, something like

for (j in names(pos_mat)) set(pos_mat, j = j, value = data[pos_mat[[j]], value])
pos_mat
#             c1         c2         c3
#  1:  1.8951935  1.3201133  1.8951935
#  2:  1.2146747 -1.7813084 -0.2842529
#  3: -2.6564554 -0.1719174 -0.1719174
#  4: -0.3066386 -0.2842529 -1.7813084
#  5: -2.4404669 -2.6564554  0.6359504
#  6: -0.1719174  1.8951935 -2.6564554
#  7:  1.3201133 -2.4404669  1.2146747
#  8:  0.6359504  0.6359504  1.3201133
#  9: -0.2842529 -0.3066386 -0.3066386
# 10: -1.7813084  1.2146747 -2.4404669

Or using matrices (using a fresh pos_mat data set)

res <- data[unlist(pos_mat), value]
dim(res) <- dim(pos_mat)
res
#             [,1]       [,2]       [,3]
#  [1,]  1.8951935  1.3201133  1.8951935
#  [2,]  1.2146747 -1.7813084 -0.2842529
#  [3,] -2.6564554 -0.1719174 -0.1719174
#  [4,] -0.3066386 -0.2842529 -1.7813084
#  [5,] -2.4404669 -2.6564554  0.6359504
#  [6,] -0.1719174  1.8951935 -2.6564554
#  [7,]  1.3201133 -2.4404669  1.2146747
#  [8,]  0.6359504  0.6359504  1.3201133
#  [9,] -0.2842529 -0.3066386 -0.3066386
# [10,] -1.7813084  1.2146747 -2.4404669

Both should be efficient, but the data.table one probably more memory efficient

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Numpy extract row, column and value from a matrix

From Dev

Filling in a matrix from a list of row,column,value

From Dev

Filling in a matrix from a list of row,column,value

From Dev

How to extract data from matrix with row and column numbers

From Dev

get column index for furthest row value to the right in a matrix

From Dev

Get a row group and column group value in an SSRS Matrix detail cell

From Dev

Get value from a certain row in a column

From Dev

Get value from dataframe based on column and row

From Dev

Get value from a certain row in a column

From Dev

get value from matrix that have same value in other column in R

From Dev

Getting row and column numbers of valid elements in a matrix

From Dev

filling a zero matrix corresponding to row and column numbers

From Dev

Get a value of a row in a column

From Dev

Get row numbers of unique rows in a matrix

From Dev

How to get a value from every column in a Numpy matrix

From Dev

Get a specific value from a specific column from specific row?

From Dev

Set a value of a specific column for each row of a matrix

From Dev

Find row and column index of maximum value in a matrix

From Dev

Get column and row names of matrix indices in a vector

From Dev

how to get value from counter Column in cassandra with multiple row keys?

From Dev

How to get last inserted row value from a column in string format?

From Java

Get row with max value from each group based on multiple column

From Dev

How to get column value from previous row for each group?

From Dev

How to get specific column and row value from multiple text files?

From Dev

how to get the row value of each column from linq

From Dev

Get specific value from table with table name, row and column index

From Dev

Get the value of a specific row and specific column from datagrid

From Dev

Get the column value <td> from a selected row <tr> by the class Attribute

From Dev

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

Related Related

  1. 1

    Numpy extract row, column and value from a matrix

  2. 2

    Filling in a matrix from a list of row,column,value

  3. 3

    Filling in a matrix from a list of row,column,value

  4. 4

    How to extract data from matrix with row and column numbers

  5. 5

    get column index for furthest row value to the right in a matrix

  6. 6

    Get a row group and column group value in an SSRS Matrix detail cell

  7. 7

    Get value from a certain row in a column

  8. 8

    Get value from dataframe based on column and row

  9. 9

    Get value from a certain row in a column

  10. 10

    get value from matrix that have same value in other column in R

  11. 11

    Getting row and column numbers of valid elements in a matrix

  12. 12

    filling a zero matrix corresponding to row and column numbers

  13. 13

    Get a value of a row in a column

  14. 14

    Get row numbers of unique rows in a matrix

  15. 15

    How to get a value from every column in a Numpy matrix

  16. 16

    Get a specific value from a specific column from specific row?

  17. 17

    Set a value of a specific column for each row of a matrix

  18. 18

    Find row and column index of maximum value in a matrix

  19. 19

    Get column and row names of matrix indices in a vector

  20. 20

    how to get value from counter Column in cassandra with multiple row keys?

  21. 21

    How to get last inserted row value from a column in string format?

  22. 22

    Get row with max value from each group based on multiple column

  23. 23

    How to get column value from previous row for each group?

  24. 24

    How to get specific column and row value from multiple text files?

  25. 25

    how to get the row value of each column from linq

  26. 26

    Get specific value from table with table name, row and column index

  27. 27

    Get the value of a specific row and specific column from datagrid

  28. 28

    Get the column value <td> from a selected row <tr> by the class Attribute

  29. 29

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

HotTag

Archive