Create a vector in a matrix using data from a dataframe conditioned to variable values in the matrix in R -


this bit hard explain in writing, i'll best. (this first post here). so, have dataframe "x" this:

+---+----+----+-----+ |   |  | b  | c   | | 1 | 50 | 40 | 30  | | 2 | 60 | 80 | 40  | | 3 | 70 | 30 | 20  | | 4 | 10 | 40 | 100 | | 5 | 35 | 50 | 20  | | 6 | 20 | 50 | 30  | +---+----+----+-----+ 

and matrix "y" this:

+---+---+---+---+---+---+ | | c | c | b | | | | 1 | 5 | 5 | 4 | 3 | 6 | +---+---+---+---+---+---+ 

(assume letters numbers, use letters explain more clearly). want 'r' create new row in "y" matrix extracting data "x" dataframe depending on values of first , second row of matrix. so, example, third column in matrix "y", extracted value dataframe 20. since on first row value "c" , on second row value "5" , in dataframe value "c" , "5" intersect 20. so, need 'r' use data in first , second row matrix , go dataframe , check first row , column each value when both conditions met , extract value in intersection, creating third row in matrix "x" containing corresponding value column. using example tables, third row should this:

+----+----+----+----+----+----+ |  | c  | c  | b  |  |  | | 1  | 5  | 5  | 4  | 3  | 6  | | 50 | 20 | 20 | 40 | 70 | 20 | +----+----+----+----+----+----+ 

i hope clear enough, think function "subset" dont know how desired result. given.

edit: following data dataframe"x"

structure(list(x = structure(c(52l, 1l, 2l, 3l, 4l, 25l, 26l,  38l, 39l, 40l, 41l, 42l, 43l, 44l, 45l, 46l, 47l, 48l, 49l, 50l,  51l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l, 13l, 14l, 15l, 16l, 17l,  18l, 19l, 20l, 21l, 22l, 23l, 24l, 27l, 28l, 29l, 30l, 31l, 32l,  33l, 34l, 35l, 36l, 37l), .label = c("0", "0.5", "1", "1.5",  "10", "10.5", "11", "11.5", "12", "12.5", "13", "13.5", "14",  "14.5", "15", "15.5", "16", "16.5", "17", "17.5", "18", "18.5",  "19", "19.5", "2", "2.5", "20", "20.5", "21", "21.5", "22", "22.5",  "23", "23.5", "24", "24.5", "25", "3", "3.5", "4", "4.5", "5",  "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "v" ), class = "factor"), ad = c(0.9, 0, 0, 0, 0, 0, 0,  1, 15, 50, 94, 147, 209, 280, 361, 455, 564, 689, 830, 978, 1130,  1281, 1431, 1579, 1728, 1872, 2011, 2144, 2263, 2353, 2418, 2462,  2489, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500), x.1 = c(0.925,  0, 0, 0, 0, 0, 0, 1, 16, 52, 97, 151, 215, 288, 372, 469, 581,  710, 854, 1006, 1161, 1315, 1467, 1619, 1770, 1915, 2055, 2189,  2300, 2381, 2439, 2477, 2496, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500), x.2 = c(0.95, 0, 0, 0, 0, 0, 0, 1, 17, 54, 100,  156, 222, 297, 383, 483, 599, 731, 879, 1034, 1192, 1348, 1503,  1657, 1810, 1956, 2096, 2230, 2331, 2405, 2455, 2488, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500), x.3 = c(0.975, 0,  0, 0, 0, 0, 0, 1, 18, 56, 104, 161, 228, 305, 394, 497, 616,  752, 903, 1061, 1222, 1381, 1539, 1696, 1849, 1996, 2135, 2260,  2354, 2421, 2465, 2491, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500), x.4 = c(1l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 19l, 58l,  107l, 165l, 234l, 314l, 405l, 511l, 634l, 773l, 929l, 1097l,  1274l, 1459l, 1649l, 1840l, 2030l, 2199l, 2327l, 2415l, 2470l,  2495l, 2500l, 2500l, 2500l, 2500l, 2500l, 2500l, 2500l, 2500l,  2500l, 2500l, 2500l, 2500l, 2500l, 2500l, 2500l, 2500l, 2500l,  2500l, 2500l, 2500l, 2500l, 2500l), x.5 = c(1.025, 0, 0, 0, 0,  0, 0, 1, 20, 60, 110, 170, 241, 322, 416, 525, 651, 795, 953,  1125, 1307, 1497, 1692, 1889, 2084, 2245, 2362, 2440, 2485, 2499,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500 ), x.6 = c(1.05, 0, 0, 0, 0, 0, 0, 1, 21, 62, 113, 175, 247,  331, 427, 539, 668, 816, 978, 1154, 1340, 1535, 1735, 1937, 2132,  2284, 2391, 2459, 2494, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500), x.7 = c(1.075, 0, 0, 0, 0, 0,  0, 1, 22, 64, 116, 179, 254, 339, 438, 553, 685, 836, 1002, 1182,  1373, 1572, 1778, 1986, 2175, 2316, 2414, 2473, 2497, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,  2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500),      x.8 = c(1.1, 0, 0, 0, 0, 0, 0, 1, 24, 66, 120, 184, 260,      348, 449, 566, 702, 856, 1026, 1211, 1406, 1610, 1821, 2035,      2217, 2349, 2437, 2486, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500), x.9 = c(1.125,      0, 0, 0, 0, 0, 0, 1, 25, 68, 123, 189, 267, 356, 460, 580,      719, 877, 1051, 1239, 1439, 1648, 1864, 2080, 2254, 2377,      2455, 2495, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500), x.10 = c(1.15, 0, 0,      0, 0, 0, 0, 1, 26, 70, 126, 193, 273, 365, 471, 594, 736,      897, 1075, 1267, 1472, 1686, 1908, 2119, 2284, 2397, 2467,      2496, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500), x.11 = c(1.175, 0, 0, 0, 0,      0, 0, 1, 27, 72, 129, 198, 279, 373, 482, 608, 753, 917,      1099, 1295, 1505, 1724, 1952, 2158, 2313, 2418, 2478, 2498,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500), x.12 = c(1.2, 0, 0, 0, 0, 0, 0,      1, 28, 74, 132, 203, 286, 382, 493, 622, 770, 937, 1123,      1324, 1537, 1761, 1995, 2197, 2343, 2439, 2489, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500), x.13 = c(1.225, 0, 0, 0, 0, 0, 0, 1, 29,      76, 136, 207, 292, 390, 504, 635, 787, 958, 1147, 1352, 1570,      1799, 2036, 2231, 2368, 2454, 2496, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500), x.14 = c(1.25, 0, 0, 0, 0, 0, 0, 1, 30, 78, 139, 212,      298, 399, 515, 649, 803, 978, 1171, 1380, 1603, 1838, 2071,      2257, 2386, 2464, 2497, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500), x.15 = c(1.275,      0, 0, 0, 0, 0, 0, 1, 31, 80, 142, 217, 305, 407, 525, 662,      820, 998, 1195, 1408, 1636, 1877, 2107, 2284, 2404, 2473,      2498, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500), x.16 = c(1.3, 0, 0,      0, 0, 0, 0, 1, 32, 82, 145, 221, 311, 415, 536, 676, 836,      1018, 1219, 1437, 1668, 1915, 2142, 2310, 2423, 2483, 2499,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,      2500, 2500, 2500, 2500, 2500)), .names = c("x", "ad",  "x.1", "x.2", "x.3", "x.4", "x.5", "x.6", "x.7", "x.8", "x.9",  "x.10", "x.11", "x.12", "x.13", "x.14", "x.15", "x.16"), class = "data.frame", row.names = c(na,  -52l)) 

i cannot paste data matrix "y" because big, 3x1644 matrix.
here first few columns of matrix "y" though.

structure(list(v1 = structure(1:3, .label = c("", "ad", "ws"), class = "factor"),      v2 = structure(c(3l, 1l, 2l), .label = c("1.2", "3.5", "v1"     ), class = "factor"), v3 = structure(c(3l, 1l, 2l), .label = c("1.2",      "4", "v2"), class = "factor"), v4 = structure(c(3l, 1l, 2l     ), .label = c("1.2", "3.5", "v3"), class = "factor")), .names = c("v1",  "v2", "v3", "v4"), class = "matrix", row.names = c(na, -3l )) 

please note matrix turned dataframe when extracted first columns post them here, still matrix in data.

try this:

rbind(y, sapply(seq_along(y),                  function(z)                    x[y[1, z], names(y)[z]])) #     c  c  b   # 1  1  5  5  4  3  6 # 2 50 20 20 40 70 20 

here, i'm subsetting using [ match values you're looking for.


for convenience of others, here's x , y:

x <- structure(list(a = c(50, 60, 70, 10, 35, 20),                      b = c(40, 80, 30, 40, 50, 50),                      c = c(30, 40, 20, 100, 20, 30)),                 .names = c("a", "b", "c"),                 row.names = c(na, -6l),                 class = "data.frame") y <- structure(list(a = 1, c = 5, c = 5, b = 4, = 3, = 6),                 .names = c("a", "c", "c", "b", "a", "a"),                 row.names = c(na, -1l), class = "data.frame") 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -