Related
In this dataset I want to get the entire row for a corresponding sample moment. Say the sample moment is t=100, I want the corresponding row of data. However, the time column does not consist of neat numbers, and I have a measurement at t = 99.7 and t= 101.1 (or something like that).
How do i get the samplemoment closest to t=100 out of my dataset?
Something like:
samplemoment = 100
test_samplemoment = test[samplemoment,]
However, since my measurements are every 2 seconds instead of every second. I'd need to give a correction:
test_samplemoment = test[(samplemoment/2),]
This does not reliably give the row of data closest to t=100 however, and I can't really come up with a better alternative that is reliable with different datasets like this.
I have the following dataset called test:
test <- structure(list(time = c(2, 3.9, 5.8, 7.8, 9.7, 11.7, 13.6, 15.5,
17.5, 19.4, 21.4, 23.3, 25.3, 27.2, 29.2, 31.2, 33.1, 35.1, 37.1,
39, 41, 42.9, 44.9, 46.8, 48.7, 50.7, 52.6, 54.6, 56.5, 58.5,
60.4, 62.4, 64.3, 66.2, 68.2, 70.1, 72.1, 74, 76, 77.9, 79.8,
81.8, 83.7, 85.7, 87.6, 89.5, 91.5, 93.4, 95.4, 97.3, 99.3, 101.2,
103.1, 105.1, 107, 109, 110.9, 112.8, 114.8, 116.7, 118.7, 120.6,
122.5, 124.5, 126.4, 128.4, 130.3, 132.2, 134.2, 136.1, 138.1,
140, 141.9, 143.9, 145.8, 147.8, 149.7, 151.6, 153.6, 155.5,
157.5, 159.4, 161.3, 163.3, 165.2, 167.2, 169.1, 171, 173, 174.9,
176.9, 178.8, 180.7, 182.7, 184.6, 186.6, 188.5, 190.4, 192.4,
194.3, 196.3, 198.2, 200.1, 202.1, 204, 206, 207.9, 209.8, 211.8,
213.7, 215.7, 217.6, 219.5, 221.5, 223.4, 225.4, 227.3, 229.3,
231.2, 233.1, 235.1, 237, 239, 240.9, 242.8, 244.8, 246.7, 248.7,
250.6, 252.5, 254.5, 256.4, 258.4, 260.3, 262.2, 264.2, 266.1,
268.1, 270, 271.9, 273.9, 275.8, 277.8, 279.7, 281.6, 283.6,
285.5, 287.5, 289.4, 291.3, 293.3, 295.2, 297.2, 299.1, 301),
v = c(14.82, 14.804, 14.82, 14.82, 14.804, 14.82, 14.812,
14.804, 14.8, 14.808, 14.8, 14.804, 15.844, 15.848, 15.848,
15.852, 15.852, 15.848, 15.852, 15.852, 15.852, 15.852, 15.856,
15.852, 15.852, 15.856, 15.856, 15.856, 15.856, 15.856, 15.856,
15.856, 15.852, 15.852, 15.852, 15.852, 15.856, 15.856, 15.856,
15.86, 15.856, 15.86, 15.864, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.856, 15.856, 15.86,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.856, 15.856, 15.856, 15.86, 15.86,
15.86, 15.856, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.856,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.856,
15.86, 15.856, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.856, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.86), a = c(1.5, 1.476, 1.5, 1.491, 1.452,
1.476, 1.478, 1.44, 1.454, 1.438, 1.442, 1.471, 0.002, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.001, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.002, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002,
0.001, 0.001, 0.001, 0.002, 0.002, 0.001, 0.002, 0.001, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.001, 0.002, 0.001, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002,
0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.002, 0.001, 0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002,
0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002), t1 = c(0, -0.0120000000000005,
-0.0200000000000014, -0.0380000000000003, -0.0400000000000009,
-0.0200000000000014, -0.0300000000000011, 0.0199999999999996,
0.00999999999999979, 0.0080000000000009, 0.00999999999999979,
0.00200000000000067, 0.0199999999999996, 0.0699999999999985,
0.0800000000000001, 0.15, 0.17, 0.198, 0.219999999999999,
0.242000000000001, 0.279999999999999, 0.34, 0.369999999999999,
0.450000000000001, 0.48, 0.508000000000001, 0.539999999999999,
0.552, 0.58, 0.629999999999999, 0.649999999999999, 0.720000000000001,
0.73, 0.747999999999999, 0.76, 0.762, 0.779999999999999,
0.819999999999999, 0.82, 0.880000000000001, 0.880000000000001,
0.888, 0.889999999999999, 0.882, 0.889999999999999, 0.92,
0.92, 0.960000000000001, 0.960000000000001, 0.958, 0.949999999999999,
0.942, 0.94, 0.969999999999999, 0.959999999999999, 1, 1,
0.988, 0.979999999999999, 0.962, 0.969999999999999, 0.989999999999998,
0.979999999999999, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.969999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999,
0.979999999999999, 0.959999999999999, 1, 0.99, 0.978, 0.969999999999999,
0.952, 0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.932, 0.93, 0.949999999999999,
0.94, 0.98, 0.970000000000001, 0.958, 0.94, 0.922000000000001,
0.92, 0.94, 0.93, 0.960000000000001, 0.950000000000001, 0.938000000000001,
0.93, 0.912000000000001, 0.91, 0.92, 0.91, 0.950000000000001,
0.940000000000001, 0.928000000000001, 0.909999999999998,
0.891999999999999, 0.889999999999999, 0.909999999999998,
0.899999999999999, 0.94, 0.92, 0.907999999999999, 0.899999999999999,
0.882, 0.879999999999999, 0.899999999999999, 0.879999999999999,
0.92, 0.91, 0.898, 0.889999999999999, 0.862, 0.859999999999999,
0.879999999999999, 0.869999999999999, 0.91, 0.9, 0.878, 0.869999999999999
), t2 = c(-0.0179999999999989, -0.0300000000000011, -0.0100000000000016,
-0.0200000000000014, 0.0199999999999996, 0.00999999999999979,
-0.00200000000000067, 0, -0.0179999999999989, -0.00999999999999979,
0.0199999999999996, 0.0299999999999994, 0.0899999999999999,
0.100000000000001, 0.118, 0.149999999999999, 0.172000000000001,
0.209999999999999, 0.27, 0.309999999999999, 0.4, 0.450000000000001,
0.488, 0.529999999999999, 0.572000000000001, 0.619999999999999,
0.68, 0.719999999999999, 0.800000000000001, 0.83, 0.858000000000001,
0.879999999999999, 0.902000000000001, 0.93, 0.979999999999999,
0.99, 1.05, 1.07, 1.078, 1.08, 1.082, 1.1, 1.13, 1.13, 1.18,
1.18, 1.178, 1.18, 1.172, 1.18, 1.2, 1.2, 1.25, 1.24, 1.228,
1.23, 1.212, 1.22, 1.24, 1.23, 1.27, 1.26, 1.258, 1.25, 1.232,
1.23, 1.25, 1.24, 1.28, 1.27, 1.258, 1.25, 1.232, 1.23, 1.25,
1.24, 1.28, 1.27, 1.248, 1.24, 1.222, 1.22, 1.24, 1.23, 1.27,
1.26, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.25, 1.24, 1.228,
1.22, 1.202, 1.19, 1.21, 1.2, 1.24, 1.23, 1.208, 1.2, 1.182,
1.18, 1.19, 1.18, 1.22, 1.21, 1.198, 1.18, 1.162, 1.16, 1.18,
1.16, 1.2, 1.19, 1.178, 1.16, 1.142, 1.14, 1.16, 1.14, 1.18,
1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.12, 1.16, 1.15, 1.138,
1.12, 1.102, 1.1, 1.12, 1.1, 1.14, 1.13, 1.108, 1.1, 1.082,
1.08, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.07
), t3 = c(-0.00999999999999979, 0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.0579999999999998,
0.0699999999999985, 0.0820000000000007, 0.0999999999999996,
0.159999999999998, 0.19, 0.26, 0.300000000000001, 0.327999999999999,
0.369999999999999, 0.391999999999999, 0.44, 0.5, 0.539999999999999,
0.620000000000001, 0.65, 0.678000000000001, 0.699999999999999,
0.722, 0.75, 0.799999999999999, 0.82, 0.880000000000001,
0.890000000000001, 0.907999999999999, 0.909999999999998,
0.912000000000001, 0.93, 0.959999999999999, 0.969999999999999,
1.02, 1.02, 1.018, 1.02, 1.012, 1.02, 1.04, 1.04, 1.09, 1.08,
1.078, 1.07, 1.052, 1.06, 1.08, 1.07, 1.11, 1.11, 1.098,
1.09, 1.072, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.08,
1.07, 1.11, 1.1, 1.078, 1.07, 1.052, 1.05, 1.07, 1.06, 1.09,
1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08, 1.07, 1.048,
1.04, 1.022, 1.02, 1.04, 1.02, 1.06, 1.05, 1.038, 1.03, 1.002,
1, 1.02, 1.01, 1.05, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.952, 0.949999999999999,
0.969999999999999, 0.959999999999999, 1, 0.98, 0.968, 0.959999999999999,
0.942, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.948, 0.94, 0.922000000000001, 0.92, 0.94, 0.92, 0.960000000000001,
0.950000000000001), t4 = c(0.0280000000000005, 0.0199999999999996,
0.00200000000000067, 0, 0.00999999999999979, 0, 0.0500000000000007,
0.0400000000000009, 0.0380000000000003, 0.0399999999999991,
0.032, 0.0499999999999989, 0.0999999999999996, 0.109999999999999,
0.19, 0.210000000000001, 0.238, 0.279999999999999, 0.302,
0.35, 0.42, 0.459999999999999, 0.56, 0.600000000000001, 0.628,
0.67, 0.702, 0.74, 0.799999999999999, 0.82, 0.9, 0.92, 0.938000000000001,
0.959999999999999, 0.972, 0.99, 1.03, 1.04, 1.1, 1.11, 1.108,
1.12, 1.112, 1.12, 1.15, 1.15, 1.2, 1.2, 1.198, 1.19, 1.182,
1.18, 1.21, 1.2, 1.24, 1.24, 1.228, 1.22, 1.212, 1.21, 1.23,
1.22, 1.26, 1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26,
1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26, 1.24, 1.228,
1.22, 1.202, 1.2, 1.22, 1.2, 1.24, 1.23, 1.218, 1.21, 1.192,
1.18, 1.2, 1.19, 1.23, 1.22, 1.198, 1.19, 1.172, 1.17, 1.19,
1.17, 1.21, 1.2, 1.188, 1.18, 1.152, 1.15, 1.17, 1.16, 1.19,
1.18, 1.168, 1.16, 1.142, 1.13, 1.15, 1.14, 1.18, 1.16, 1.148,
1.14, 1.122, 1.11, 1.13, 1.12, 1.16, 1.14, 1.128, 1.12, 1.102,
1.09, 1.11, 1.1, 1.14, 1.12, 1.108, 1.1, 1.082, 1.07, 1.09,
1.08, 1.12, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07, 1.06, 1.1,
1.09, 1.068, 1.06, 1.042), t5 = c(0, 0.0199999999999996,
0.00999999999999979, 0.0500000000000007, 0.0400000000000009,
0.0280000000000005, 0.0299999999999994, 0.0120000000000005,
0.0199999999999996, 0.0599999999999987, 0.0599999999999987,
0.130000000000001, 0.140000000000001, 0.157999999999999,
0.18, 0.202, 0.24, 0.299999999999999, 0.33, 0.42, 0.450000000000001,
0.488, 0.52, 0.552, 0.59, 0.649999999999999, 0.68, 0.76,
0.790000000000001, 0.808, 0.83, 0.842000000000001, 0.869999999999999,
0.909999999999998, 0.93, 0.99, 1, 1.008, 1.01, 1.012, 1.03,
1.06, 1.06, 1.11, 1.11, 1.108, 1.11, 1.102, 1.11, 1.13, 1.13,
1.17, 1.17, 1.158, 1.16, 1.142, 1.14, 1.17, 1.16, 1.2, 1.19,
1.178, 1.17, 1.162, 1.16, 1.18, 1.17, 1.21, 1.2, 1.188, 1.18,
1.162, 1.16, 1.17, 1.16, 1.2, 1.19, 1.178, 1.17, 1.152, 1.15,
1.17, 1.15, 1.19, 1.18, 1.168, 1.16, 1.142, 1.14, 1.15, 1.14,
1.18, 1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.13, 1.17, 1.15,
1.138, 1.13, 1.112, 1.1, 1.12, 1.11, 1.15, 1.14, 1.118, 1.11,
1.092, 1.09, 1.11, 1.09, 1.13, 1.12, 1.108, 1.09, 1.072,
1.07, 1.09, 1.08, 1.11, 1.1, 1.088, 1.08, 1.062, 1.05, 1.07,
1.06, 1.1, 1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08,
1.07, 1.048, 1.04, 1.022, 1.02, 1.03, 1.02, 1.06, 1.05, 1.038,
1.02, 1.002, 1, 1.02, 1), t6 = c(0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.048, 0.0599999999999987,
0.072000000000001, 0.0899999999999999, 0.139999999999999,
0.16, 0.24, 0.270000000000001, 0.288, 0.319999999999999,
0.352, 0.389999999999999, 0.449999999999999, 0.479999999999999,
0.56, 0.59, 0.608000000000001, 0.639999999999999, 0.652000000000001,
0.68, 0.729999999999999, 0.75, 0.81, 0.83, 0.838000000000001,
0.85, 0.852, 0.869999999999999, 0.909999999999998, 0.91,
0.970000000000001, 0.970000000000001, 0.968, 0.979999999999999,
0.972, 0.98, 1.01, 1.01, 1.05, 1.05, 1.048, 1.04, 1.032,
1.04, 1.06, 1.06, 1.1, 1.09, 1.088, 1.08, 1.062, 1.06, 1.09,
1.08, 1.12, 1.11, 1.098, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13,
1.12, 1.108, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13, 1.12, 1.108,
1.1, 1.082, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.08, 1.07, 1.11, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07,
1.06, 1.1, 1.09, 1.078, 1.07, 1.042, 1.04, 1.06, 1.05, 1.09,
1.07, 1.058, 1.05, 1.032, 1.03, 1.05, 1.03, 1.07, 1.06, 1.048,
1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.04, 1.028, 1.02, 1.002,
1, 1.01, 1, 1.04, 1.03, 1.018, 1, 0.982000000000001, 0.98,
1, 0.99, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.959999999999999, 0.979999999999999, 0.969999999999999,
1.01, 1, 0.978), t7 = c(0.00999999999999979, -0.00799999999999912,
-0.00999999999999979, 0.00999999999999979, 0, 0.0400000000000009,
0.0300000000000011, 0.0180000000000007, 0.0199999999999996,
0.0120000000000005, 0.0199999999999996, 0.0499999999999989,
0.0599999999999987, 0.120000000000001, 0.140000000000001,
0.148, 0.17, 0.182, 0.209999999999999, 0.27, 0.289999999999999,
0.370000000000001, 0.390000000000001, 0.417999999999999,
0.44, 0.462, 0.49, 0.539999999999999, 0.57, 0.640000000000001,
0.65, 0.667999999999999, 0.69, 0.692, 0.709999999999999,
0.75, 0.77, 0.82, 0.83, 0.838000000000001, 0.84, 0.842000000000001,
0.85, 0.889999999999999, 0.889999999999999, 0.94, 0.940000000000001,
0.938000000000001, 0.94, 0.932, 0.94, 0.969999999999999,
0.969999999999999, 1.01, 1.01, 0.997999999999999, 1, 0.992000000000001,
0.99, 1.02, 1.01, 1.05, 1.05, 1.038, 1.03, 1.022, 1.02, 1.04,
1.04, 1.08, 1.07, 1.058, 1.05, 1.032, 1.04, 1.06, 1.05, 1.09,
1.08, 1.068, 1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068,
1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.042,
1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.032, 1.03, 1.05,
1.04, 1.08, 1.07, 1.058, 1.05, 1.022, 1.02, 1.04, 1.03, 1.07,
1.06, 1.048, 1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.05, 1.028,
1.02, 1.002, 1, 1.02, 1.01, 1.04, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 1.008, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999
), t8 = c(-0.0300000000000011, -0.0400000000000009, 0, -0.00999999999999979,
-0.0220000000000002, -0.0300000000000011, -0.0380000000000003,
-0.0400000000000009, -0.0100000000000016, -0.00999999999999979,
0.0400000000000009, 0.0500000000000007, 0.0579999999999998,
0.0800000000000001, 0.0920000000000005, 0.119999999999999,
0.17, 0.199999999999999, 0.280000000000001, 0.31, 0.338000000000001,
0.369999999999999, 0.382, 0.42, 0.479999999999999, 0.5, 0.57,
0.600000000000001, 0.618, 0.629999999999999, 0.641999999999999,
0.66, 0.709999999999999, 0.719999999999999, 0.780000000000001,
0.780000000000001, 0.788, 0.799999999999999, 0.792, 0.799999999999999,
0.84, 0.84, 0.890000000000001, 0.890000000000001, 0.888,
0.889999999999999, 0.872, 0.879999999999999, 0.909999999999998,
0.899999999999999, 0.950000000000001, 0.940000000000001,
0.938000000000001, 0.93, 0.922000000000001, 0.92, 0.94, 0.94,
0.98, 0.970000000000001, 0.958, 0.959999999999999, 0.942,
0.94, 0.959999999999999, 0.949999999999999, 0.99, 0.98, 0.968,
0.969999999999999, 0.952, 0.949999999999999, 0.969999999999999,
0.959999999999999, 1, 0.99, 0.978, 0.969999999999999, 0.952,
0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.942, 0.94, 0.959999999999999,
0.949999999999999, 0.99, 0.98, 0.958, 0.949999999999999,
0.932, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.958, 0.94, 0.922000000000001, 0.92, 0.94, 0.93, 0.970000000000001,
0.960000000000001, 0.938000000000001, 0.93, 0.912000000000001,
0.91, 0.93, 0.92, 0.950000000000001, 0.940000000000001, 0.928000000000001,
0.92, 0.902000000000001, 0.9, 0.909999999999998, 0.899999999999999,
0.94, 0.93, 0.917999999999999, 0.909999999999998, 0.891999999999999,
0.879999999999999, 0.899999999999999, 0.889999999999999,
0.93, 0.92, 0.907999999999999, 0.889999999999999, 0.872,
0.869999999999999, 0.889999999999999, 0.879999999999999,
0.92, 0.9, 0.888, 0.879999999999999, 0.862, 0.859999999999999,
0.869999999999999, 0.859999999999999, 0.9, 0.890000000000001,
0.878, 0.869999999999999, 0.852, 0.84, 0.859999999999999,
0.85, 0.890000000000001)), row.names = c(NA, -155L), class = "data.frame")
You can also use which.min() from base R.
test[which.min(abs(100-test$time)),]
time v a t1 t2 t3 t4 t5 t6 t7 t8
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
51 99.3 15.86 0.001 0.95 1.2 1.08 1.182 1.13 1.048 0.94 0.95
This will return a row from the test -dataframe that has a value closest to the value of 100 in the column time.
We could compare the absolut difference of time and 100 with the min of this difference:
library(dplyr)
test %>%
filter(abs(time-100)==min(abs(time-100)))
time v a t1 t2 t3 t4 t5 t6 t7 t8
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 99.3 15.9 0.001 0.950 1.2 1.08 1.18 1.13 1.05 0.94 0.950
I need to calculate the N variable for each row.
This is done by multiplying the values in the (horizontal) row with the vector Nangle (which is of the same length as test[,c(4:11)]) and getting the sum of these 8 multiplications. It works for a single row as follows
sum(test[1,c(4:11)] * Nangle)
However, I need to do this calculation for every row, and add the results as a new (vertical) column 'N'
I tried the following:
test$N = sum(test[,c(4:11)] * Nangle)
This does create the new column, but just adds the same number after every row, so somehow the calculation is only done for the total of all of the rows and not per row.
How do I make this iterate over all rows?
The dataset test:
test <- structure(list(time = c(2, 3.9, 5.8, 7.8, 9.7, 11.7, 13.6, 15.5,
17.5, 19.4, 21.4, 23.3, 25.3, 27.2, 29.2, 31.2, 33.1, 35.1, 37.1,
39, 41, 42.9, 44.9, 46.8, 48.7, 50.7, 52.6, 54.6, 56.5, 58.5,
60.4, 62.4, 64.3, 66.2, 68.2, 70.1, 72.1, 74, 76, 77.9, 79.8,
81.8, 83.7, 85.7, 87.6, 89.5, 91.5, 93.4, 95.4, 97.3, 99.3, 101.2,
103.1, 105.1, 107, 109, 110.9, 112.8, 114.8, 116.7, 118.7, 120.6,
122.5, 124.5, 126.4, 128.4, 130.3, 132.2, 134.2, 136.1, 138.1,
140, 141.9, 143.9, 145.8, 147.8, 149.7, 151.6, 153.6, 155.5,
157.5, 159.4, 161.3, 163.3, 165.2, 167.2, 169.1, 171, 173, 174.9,
176.9, 178.8, 180.7, 182.7, 184.6, 186.6, 188.5, 190.4, 192.4,
194.3, 196.3, 198.2, 200.1, 202.1, 204, 206, 207.9, 209.8, 211.8,
213.7, 215.7, 217.6, 219.5, 221.5, 223.4, 225.4, 227.3, 229.3,
231.2, 233.1, 235.1, 237, 239, 240.9, 242.8, 244.8, 246.7, 248.7,
250.6, 252.5, 254.5, 256.4, 258.4, 260.3, 262.2, 264.2, 266.1,
268.1, 270, 271.9, 273.9, 275.8, 277.8, 279.7, 281.6, 283.6,
285.5, 287.5, 289.4, 291.3, 293.3, 295.2, 297.2, 299.1, 301),
v = c(14.82, 14.804, 14.82, 14.82, 14.804, 14.82, 14.812,
14.804, 14.8, 14.808, 14.8, 14.804, 15.844, 15.848, 15.848,
15.852, 15.852, 15.848, 15.852, 15.852, 15.852, 15.852, 15.856,
15.852, 15.852, 15.856, 15.856, 15.856, 15.856, 15.856, 15.856,
15.856, 15.852, 15.852, 15.852, 15.852, 15.856, 15.856, 15.856,
15.86, 15.856, 15.86, 15.864, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.856, 15.856, 15.86,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.856, 15.856, 15.856, 15.86, 15.86,
15.86, 15.856, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.856, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.864, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.856,
15.86, 15.86, 15.86, 15.864, 15.86, 15.86, 15.86, 15.856,
15.86, 15.856, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.856, 15.86,
15.856, 15.86, 15.856, 15.86, 15.86, 15.864, 15.86, 15.86,
15.86, 15.86, 15.86, 15.86, 15.856, 15.86, 15.86, 15.86,
15.856, 15.86, 15.86), a = c(1.5, 1.476, 1.5, 1.491, 1.452,
1.476, 1.478, 1.44, 1.454, 1.438, 1.442, 1.471, 0.002, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.001, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002, 0.002,
0.002, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.002,
0.001, 0.001, 0.001, 0.002, 0.002, 0.001, 0.002, 0.001, 0.002,
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.001, 0.002, 0.001, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002,
0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.002, 0.001, 0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002,
0.001, 0.002, 0.001, 0.002, 0.002, 0.002, 0.001, 0.001, 0.002,
0.002, 0.002, 0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002,
0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.001,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002,
0.002, 0.002, 0.001, 0.002, 0.002, 0.002), t1 = c(0, -0.0120000000000005,
-0.0200000000000014, -0.0380000000000003, -0.0400000000000009,
-0.0200000000000014, -0.0300000000000011, 0.0199999999999996,
0.00999999999999979, 0.0080000000000009, 0.00999999999999979,
0.00200000000000067, 0.0199999999999996, 0.0699999999999985,
0.0800000000000001, 0.15, 0.17, 0.198, 0.219999999999999,
0.242000000000001, 0.279999999999999, 0.34, 0.369999999999999,
0.450000000000001, 0.48, 0.508000000000001, 0.539999999999999,
0.552, 0.58, 0.629999999999999, 0.649999999999999, 0.720000000000001,
0.73, 0.747999999999999, 0.76, 0.762, 0.779999999999999,
0.819999999999999, 0.82, 0.880000000000001, 0.880000000000001,
0.888, 0.889999999999999, 0.882, 0.889999999999999, 0.92,
0.92, 0.960000000000001, 0.960000000000001, 0.958, 0.949999999999999,
0.942, 0.94, 0.969999999999999, 0.959999999999999, 1, 1,
0.988, 0.979999999999999, 0.962, 0.969999999999999, 0.989999999999998,
0.979999999999999, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1.01, 0.997999999999999, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.969999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999,
0.979999999999999, 0.959999999999999, 1, 0.99, 0.978, 0.969999999999999,
0.952, 0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.932, 0.93, 0.949999999999999,
0.94, 0.98, 0.970000000000001, 0.958, 0.94, 0.922000000000001,
0.92, 0.94, 0.93, 0.960000000000001, 0.950000000000001, 0.938000000000001,
0.93, 0.912000000000001, 0.91, 0.92, 0.91, 0.950000000000001,
0.940000000000001, 0.928000000000001, 0.909999999999998,
0.891999999999999, 0.889999999999999, 0.909999999999998,
0.899999999999999, 0.94, 0.92, 0.907999999999999, 0.899999999999999,
0.882, 0.879999999999999, 0.899999999999999, 0.879999999999999,
0.92, 0.91, 0.898, 0.889999999999999, 0.862, 0.859999999999999,
0.879999999999999, 0.869999999999999, 0.91, 0.9, 0.878, 0.869999999999999
), t2 = c(-0.0179999999999989, -0.0300000000000011, -0.0100000000000016,
-0.0200000000000014, 0.0199999999999996, 0.00999999999999979,
-0.00200000000000067, 0, -0.0179999999999989, -0.00999999999999979,
0.0199999999999996, 0.0299999999999994, 0.0899999999999999,
0.100000000000001, 0.118, 0.149999999999999, 0.172000000000001,
0.209999999999999, 0.27, 0.309999999999999, 0.4, 0.450000000000001,
0.488, 0.529999999999999, 0.572000000000001, 0.619999999999999,
0.68, 0.719999999999999, 0.800000000000001, 0.83, 0.858000000000001,
0.879999999999999, 0.902000000000001, 0.93, 0.979999999999999,
0.99, 1.05, 1.07, 1.078, 1.08, 1.082, 1.1, 1.13, 1.13, 1.18,
1.18, 1.178, 1.18, 1.172, 1.18, 1.2, 1.2, 1.25, 1.24, 1.228,
1.23, 1.212, 1.22, 1.24, 1.23, 1.27, 1.26, 1.258, 1.25, 1.232,
1.23, 1.25, 1.24, 1.28, 1.27, 1.258, 1.25, 1.232, 1.23, 1.25,
1.24, 1.28, 1.27, 1.248, 1.24, 1.222, 1.22, 1.24, 1.23, 1.27,
1.26, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.25, 1.24, 1.228,
1.22, 1.202, 1.19, 1.21, 1.2, 1.24, 1.23, 1.208, 1.2, 1.182,
1.18, 1.19, 1.18, 1.22, 1.21, 1.198, 1.18, 1.162, 1.16, 1.18,
1.16, 1.2, 1.19, 1.178, 1.16, 1.142, 1.14, 1.16, 1.14, 1.18,
1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.12, 1.16, 1.15, 1.138,
1.12, 1.102, 1.1, 1.12, 1.1, 1.14, 1.13, 1.108, 1.1, 1.082,
1.08, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.07
), t3 = c(-0.00999999999999979, 0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.0579999999999998,
0.0699999999999985, 0.0820000000000007, 0.0999999999999996,
0.159999999999998, 0.19, 0.26, 0.300000000000001, 0.327999999999999,
0.369999999999999, 0.391999999999999, 0.44, 0.5, 0.539999999999999,
0.620000000000001, 0.65, 0.678000000000001, 0.699999999999999,
0.722, 0.75, 0.799999999999999, 0.82, 0.880000000000001,
0.890000000000001, 0.907999999999999, 0.909999999999998,
0.912000000000001, 0.93, 0.959999999999999, 0.969999999999999,
1.02, 1.02, 1.018, 1.02, 1.012, 1.02, 1.04, 1.04, 1.09, 1.08,
1.078, 1.07, 1.052, 1.06, 1.08, 1.07, 1.11, 1.11, 1.098,
1.09, 1.072, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.09, 1.08, 1.12, 1.11, 1.088, 1.08, 1.062, 1.06, 1.08,
1.07, 1.11, 1.1, 1.078, 1.07, 1.052, 1.05, 1.07, 1.06, 1.09,
1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08, 1.07, 1.048,
1.04, 1.022, 1.02, 1.04, 1.02, 1.06, 1.05, 1.038, 1.03, 1.002,
1, 1.02, 1.01, 1.05, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 0.997999999999999, 0.989999999999998,
0.972, 0.969999999999999, 0.989999999999998, 0.979999999999999,
1.01, 1, 0.988, 0.979999999999999, 0.952, 0.949999999999999,
0.969999999999999, 0.959999999999999, 1, 0.98, 0.968, 0.959999999999999,
0.942, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.948, 0.94, 0.922000000000001, 0.92, 0.94, 0.92, 0.960000000000001,
0.950000000000001), t4 = c(0.0280000000000005, 0.0199999999999996,
0.00200000000000067, 0, 0.00999999999999979, 0, 0.0500000000000007,
0.0400000000000009, 0.0380000000000003, 0.0399999999999991,
0.032, 0.0499999999999989, 0.0999999999999996, 0.109999999999999,
0.19, 0.210000000000001, 0.238, 0.279999999999999, 0.302,
0.35, 0.42, 0.459999999999999, 0.56, 0.600000000000001, 0.628,
0.67, 0.702, 0.74, 0.799999999999999, 0.82, 0.9, 0.92, 0.938000000000001,
0.959999999999999, 0.972, 0.99, 1.03, 1.04, 1.1, 1.11, 1.108,
1.12, 1.112, 1.12, 1.15, 1.15, 1.2, 1.2, 1.198, 1.19, 1.182,
1.18, 1.21, 1.2, 1.24, 1.24, 1.228, 1.22, 1.212, 1.21, 1.23,
1.22, 1.26, 1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26,
1.25, 1.238, 1.23, 1.212, 1.21, 1.23, 1.22, 1.26, 1.24, 1.228,
1.22, 1.202, 1.2, 1.22, 1.2, 1.24, 1.23, 1.218, 1.21, 1.192,
1.18, 1.2, 1.19, 1.23, 1.22, 1.198, 1.19, 1.172, 1.17, 1.19,
1.17, 1.21, 1.2, 1.188, 1.18, 1.152, 1.15, 1.17, 1.16, 1.19,
1.18, 1.168, 1.16, 1.142, 1.13, 1.15, 1.14, 1.18, 1.16, 1.148,
1.14, 1.122, 1.11, 1.13, 1.12, 1.16, 1.14, 1.128, 1.12, 1.102,
1.09, 1.11, 1.1, 1.14, 1.12, 1.108, 1.1, 1.082, 1.07, 1.09,
1.08, 1.12, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07, 1.06, 1.1,
1.09, 1.068, 1.06, 1.042), t5 = c(0, 0.0199999999999996,
0.00999999999999979, 0.0500000000000007, 0.0400000000000009,
0.0280000000000005, 0.0299999999999994, 0.0120000000000005,
0.0199999999999996, 0.0599999999999987, 0.0599999999999987,
0.130000000000001, 0.140000000000001, 0.157999999999999,
0.18, 0.202, 0.24, 0.299999999999999, 0.33, 0.42, 0.450000000000001,
0.488, 0.52, 0.552, 0.59, 0.649999999999999, 0.68, 0.76,
0.790000000000001, 0.808, 0.83, 0.842000000000001, 0.869999999999999,
0.909999999999998, 0.93, 0.99, 1, 1.008, 1.01, 1.012, 1.03,
1.06, 1.06, 1.11, 1.11, 1.108, 1.11, 1.102, 1.11, 1.13, 1.13,
1.17, 1.17, 1.158, 1.16, 1.142, 1.14, 1.17, 1.16, 1.2, 1.19,
1.178, 1.17, 1.162, 1.16, 1.18, 1.17, 1.21, 1.2, 1.188, 1.18,
1.162, 1.16, 1.17, 1.16, 1.2, 1.19, 1.178, 1.17, 1.152, 1.15,
1.17, 1.15, 1.19, 1.18, 1.168, 1.16, 1.142, 1.14, 1.15, 1.14,
1.18, 1.17, 1.158, 1.14, 1.122, 1.12, 1.14, 1.13, 1.17, 1.15,
1.138, 1.13, 1.112, 1.1, 1.12, 1.11, 1.15, 1.14, 1.118, 1.11,
1.092, 1.09, 1.11, 1.09, 1.13, 1.12, 1.108, 1.09, 1.072,
1.07, 1.09, 1.08, 1.11, 1.1, 1.088, 1.08, 1.062, 1.05, 1.07,
1.06, 1.1, 1.08, 1.068, 1.06, 1.042, 1.03, 1.05, 1.04, 1.08,
1.07, 1.048, 1.04, 1.022, 1.02, 1.03, 1.02, 1.06, 1.05, 1.038,
1.02, 1.002, 1, 1.02, 1), t6 = c(0.0300000000000011, 0.0200000000000014,
0.0080000000000009, 0, -0.0179999999999989, -0.0199999999999996,
0, 0, 0.0500000000000007, 0.0500000000000007, 0.048, 0.0599999999999987,
0.072000000000001, 0.0899999999999999, 0.139999999999999,
0.16, 0.24, 0.270000000000001, 0.288, 0.319999999999999,
0.352, 0.389999999999999, 0.449999999999999, 0.479999999999999,
0.56, 0.59, 0.608000000000001, 0.639999999999999, 0.652000000000001,
0.68, 0.729999999999999, 0.75, 0.81, 0.83, 0.838000000000001,
0.85, 0.852, 0.869999999999999, 0.909999999999998, 0.91,
0.970000000000001, 0.970000000000001, 0.968, 0.979999999999999,
0.972, 0.98, 1.01, 1.01, 1.05, 1.05, 1.048, 1.04, 1.032,
1.04, 1.06, 1.06, 1.1, 1.09, 1.088, 1.08, 1.062, 1.06, 1.09,
1.08, 1.12, 1.11, 1.098, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13,
1.12, 1.108, 1.1, 1.082, 1.08, 1.1, 1.09, 1.13, 1.12, 1.108,
1.1, 1.082, 1.07, 1.09, 1.08, 1.12, 1.11, 1.098, 1.09, 1.072,
1.07, 1.08, 1.07, 1.11, 1.1, 1.088, 1.08, 1.062, 1.06, 1.07,
1.06, 1.1, 1.09, 1.078, 1.07, 1.042, 1.04, 1.06, 1.05, 1.09,
1.07, 1.058, 1.05, 1.032, 1.03, 1.05, 1.03, 1.07, 1.06, 1.048,
1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.04, 1.028, 1.02, 1.002,
1, 1.01, 1, 1.04, 1.03, 1.018, 1, 0.982000000000001, 0.98,
1, 0.99, 1.02, 1.01, 0.997999999999999, 0.989999999999998,
0.972, 0.959999999999999, 0.979999999999999, 0.969999999999999,
1.01, 1, 0.978), t7 = c(0.00999999999999979, -0.00799999999999912,
-0.00999999999999979, 0.00999999999999979, 0, 0.0400000000000009,
0.0300000000000011, 0.0180000000000007, 0.0199999999999996,
0.0120000000000005, 0.0199999999999996, 0.0499999999999989,
0.0599999999999987, 0.120000000000001, 0.140000000000001,
0.148, 0.17, 0.182, 0.209999999999999, 0.27, 0.289999999999999,
0.370000000000001, 0.390000000000001, 0.417999999999999,
0.44, 0.462, 0.49, 0.539999999999999, 0.57, 0.640000000000001,
0.65, 0.667999999999999, 0.69, 0.692, 0.709999999999999,
0.75, 0.77, 0.82, 0.83, 0.838000000000001, 0.84, 0.842000000000001,
0.85, 0.889999999999999, 0.889999999999999, 0.94, 0.940000000000001,
0.938000000000001, 0.94, 0.932, 0.94, 0.969999999999999,
0.969999999999999, 1.01, 1.01, 0.997999999999999, 1, 0.992000000000001,
0.99, 1.02, 1.01, 1.05, 1.05, 1.038, 1.03, 1.022, 1.02, 1.04,
1.04, 1.08, 1.07, 1.058, 1.05, 1.032, 1.04, 1.06, 1.05, 1.09,
1.08, 1.068, 1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068,
1.06, 1.042, 1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.042,
1.04, 1.06, 1.05, 1.09, 1.08, 1.068, 1.06, 1.032, 1.03, 1.05,
1.04, 1.08, 1.07, 1.058, 1.05, 1.022, 1.02, 1.04, 1.03, 1.07,
1.06, 1.048, 1.04, 1.012, 1.01, 1.03, 1.02, 1.06, 1.05, 1.028,
1.02, 1.002, 1, 1.02, 1.01, 1.04, 1.03, 1.018, 1.01, 0.992000000000001,
0.99, 1, 0.99, 1.03, 1.02, 1.008, 0.989999999999998, 0.972,
0.969999999999999, 0.989999999999998, 0.979999999999999,
1.02, 1, 0.988, 0.979999999999999, 0.962, 0.959999999999999
), t8 = c(-0.0300000000000011, -0.0400000000000009, 0, -0.00999999999999979,
-0.0220000000000002, -0.0300000000000011, -0.0380000000000003,
-0.0400000000000009, -0.0100000000000016, -0.00999999999999979,
0.0400000000000009, 0.0500000000000007, 0.0579999999999998,
0.0800000000000001, 0.0920000000000005, 0.119999999999999,
0.17, 0.199999999999999, 0.280000000000001, 0.31, 0.338000000000001,
0.369999999999999, 0.382, 0.42, 0.479999999999999, 0.5, 0.57,
0.600000000000001, 0.618, 0.629999999999999, 0.641999999999999,
0.66, 0.709999999999999, 0.719999999999999, 0.780000000000001,
0.780000000000001, 0.788, 0.799999999999999, 0.792, 0.799999999999999,
0.84, 0.84, 0.890000000000001, 0.890000000000001, 0.888,
0.889999999999999, 0.872, 0.879999999999999, 0.909999999999998,
0.899999999999999, 0.950000000000001, 0.940000000000001,
0.938000000000001, 0.93, 0.922000000000001, 0.92, 0.94, 0.94,
0.98, 0.970000000000001, 0.958, 0.959999999999999, 0.942,
0.94, 0.959999999999999, 0.949999999999999, 0.99, 0.98, 0.968,
0.969999999999999, 0.952, 0.949999999999999, 0.969999999999999,
0.959999999999999, 1, 0.99, 0.978, 0.969999999999999, 0.952,
0.949999999999999, 0.959999999999999, 0.949999999999999,
0.99, 0.98, 0.968, 0.959999999999999, 0.942, 0.94, 0.959999999999999,
0.949999999999999, 0.99, 0.98, 0.958, 0.949999999999999,
0.932, 0.93, 0.949999999999999, 0.94, 0.98, 0.970000000000001,
0.958, 0.94, 0.922000000000001, 0.92, 0.94, 0.93, 0.970000000000001,
0.960000000000001, 0.938000000000001, 0.93, 0.912000000000001,
0.91, 0.93, 0.92, 0.950000000000001, 0.940000000000001, 0.928000000000001,
0.92, 0.902000000000001, 0.9, 0.909999999999998, 0.899999999999999,
0.94, 0.93, 0.917999999999999, 0.909999999999998, 0.891999999999999,
0.879999999999999, 0.899999999999999, 0.889999999999999,
0.93, 0.92, 0.907999999999999, 0.889999999999999, 0.872,
0.869999999999999, 0.889999999999999, 0.879999999999999,
0.92, 0.9, 0.888, 0.879999999999999, 0.862, 0.859999999999999,
0.869999999999999, 0.859999999999999, 0.9, 0.890000000000001,
0.878, 0.869999999999999, 0.852, 0.84, 0.859999999999999,
0.85, 0.890000000000001)), row.names = c(NA, -155L), class = "data.frame")
Nangle <- c(1, 0.707106781186548, 6.12303176911189e-17, -0.707106781186547,
-1, -0.707106781186548, -1.83690953073357e-16, 0.707106781186547
)
I probably need to fill in something before the , between the brackets, but can't seem to figure out what exactly.
This works:
test$N <- colSums(t(test[, 4:11]) * Nangle)
However, I would be careful about using column numbers in the code when you can easily refer to the appropriate columns by their names, e.g. like this:
test$N <- colSums(t(test[, paste0('t', 1:8)]) * Nangle)
I have performed Spearman correlation for my data. Then I tried to cluster and plot my data using the "ward.D2" method for corrplot()and pheatmap(). However, the order of the variables is different between the two plots.
Could someone help me clarify this point, thus correcting my code and creating the two plots with the same order of clustered variables?
Thank you so much.
#A sample of my dataset:
dput(Data_corr)
structure(list(S_cHDLP = c(0.299999999999999, -2.78, 0.880000000000001,
2.48, 2.15, 5.31, 3.02, 1.19, 2.1, -1.18, -0.34, 1.25, -3.25,
-3.16, 0.19, -0.100000000000001, -2.16, -0.220000000000001, 0.77,
-2.12), H7P = c(-0.18, -0.48, -0.13, -0.21, 0.07, 0.64, -0.13,
-0.1, 0.12, -0.22, 0.09, -0.0399999999999999, -1.56, 0.39, 0.58,
-0.49, 0.2, 0.13, 0.11, 0.06), H6P = c(0, 0, 0, 0.16, -0.23,
0, 0, 0, -0.26, -0.28, 0.06, -0.17, 1.16, -0.12, -0.32, -0.17,
0.38, 0.05, 0.01, 0), H5P = c(0, 0.84, 0.47, 1.21, 0.01, 0.21,
1.36, 0.2, -0.12, 0.93, -1.01, 0, -0.58, -0.97, -1, 0.97, -0.89,
0.35, -0.59, -0.12), H4P = c(-0.12, -1.27, -0.18, 0.25, 1.02,
1.26, -0.62, -0.16, 0.25, -0.01, 0.44, 0.17, 0.19, 0.97, 2.35,
0.3, -0.18, 0.03, 0.0899999999999999, 0.38), H3P = c(-0.31, 0.39,
0.13, 0.29, 0, 0.02, -0.07, 0, 0, -0.32, 0, -0.79, 0, -0.53,
-0.71, -0.2, 2.08, 0.86, 0, 0), H2P = c(-1.28, -0.619999999999999,
-1.07, 1.96, 0.15, 4.92, 1.55, 3, -0.459999999999999, -0.56,
1.12, 3.44, -1.48, -1.27, 1.45, 0.609999999999999, -1.59, -1.57,
2.04, 2.03), H1P = c(1.58, -2.15, 1.96, 0.51, 2, 0.37, 1.47,
-1.83, 2.56, -0.62, -1.46, -2.19, -1.77, -1.9, -1.25, -0.73,
-0.57, 1.35, -1.28, -4.14), TRLZ_TRL = c(4.61, 1.49, -2.71, 1.54,
-5.46, 2.18, 3.48, 12.83, 7.51, 7.74, -8.38, -0.729999999999997,
6.11, -19.74, -0.869999999999997, -1.82, -1.57000000000001, 0.609999999999999,
-14.79, -18.65), LDLZ = c(-0.0599999999999987, -0.400000000000002,
-0.289999999999999, -1.2, -0.479999999999997, -0.59, -1.29, -0.0599999999999987,
0.210000000000001, -1.58, 1.97, 0.0800000000000018, -1, 1.95,
1.41, 0.00999999999999801, 0.430000000000003, -0.289999999999999,
0.68, 0.52), HDLZ = c(-0.200000000000001, -0.200000000000001,
-0.0700000000000003, 0, -0.0200000000000014, -0.0199999999999996,
-0.0399999999999991, -0.119999999999999, -0.0900000000000016,
-0.0500000000000007, -0.15, -0.16, -0.640000000000001, 0.42,
0.16, -0.130000000000001, 0.15, 0.41, -0.0300000000000011, 0.18
)), class = "data.frame", row.names = c(NA, -20L))
library(pheatmap)
library(corrplot)
CorMethod <- "spearman"
CorMatrix <- cor(Data_corr, method=CorMethod, use="pairwise.complete.obs")
## 1st Plot
Plot3<-pheatmap(CorMatrix, cluster_cols=T, cluster_rows=T, cutree_rows = 3, angle_col=45, fontsize_col=5, fontsize_row = 7, treeheight_col=0, clustering_method="ward.D2")
#2nd Plot
Plot8 <-corrplot(CorMatrix, method="square", type="lower", order="hclust", hclust.method="ward.D2", tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45)
You can create a corrplot with the same order given by pheatmap as follows:
#2nd Plot
library(RColorBrewer)
ord <- Plot3$tree_row$order
ReordCorMatrix <- CorMatrix[ord, ord]
Plot8 <-corrplot(ReordCorMatrix, method="square", type="lower", order="original",
hclust.method="ward.D2",
tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45,
col=colorRampPalette(rev(brewer.pal(n = 7, name="RdYlBu")))(100))
I managed to run a while loop to run t-tests comparing each row (see code below) & the results print out in the console. Now I am wondering how to export the t-test results from r? Is it possible to add a new column to the original dataframe with the t-test & p-value results?
`a<-1 #row1
while a<11 #number of rows(10)+1
{
b<-t.test(Large_Intestine_WT[a,],Large_Intestine_ALL_MUTANT[a,],paired=FALSE)
print(rownames(Large_Intestine_WT[a,])) print (b) a<-a+1
}`
E.g.
dataframe1:
dput(Large_Intestine_WT[1:10, 1:6])
structure(list(SKCO1_LARGE_INTESTINE = c(50.66, 0, 63.12, 5.57,
9.82, 0, 0.03, 59.69, 54.33, 15.95), SW1463_LARGE_INTESTINE = c(11.99,
0, 158.81, 4.21, 5.61, 0.98, 0.16, 65.32, 69.42, 13.85), C2BBE1_LARGE_INTESTINE = c(90.01,
0.07, 251.76, 3.75, 21.49, 0, 0.02, 135.1, 51.98, 23.82), LS123_LARGE_INTESTINE = c(41.13,
0.02, 83.12, 3.45, 14.64, 0.08, 0.25, 62.92, 14.1, 17.93), LS513_LARGE_INTESTINE = c(15.75,
0, 58.3, 7.28, 12.55, 0.02, 0.01, 46.24, 30.43, 14.89), MDST8_LARGE_INTESTINE = c(16.32,
0, 76.72, 2.69, 16.29, 0.01, 11.71, 86.57, 5.35, 19.58)), row.names = c("ENSG00000000003.10",
"ENSG00000000005.5", "ENSG00000000419.8", "ENSG00000000457.9",
"ENSG00000000460.12", "ENSG00000000938.8", "ENSG00000000971.11",
"ENSG00000001036.9", "ENSG00000001084.6", "ENSG00000001167.10"
), class = "data.frame")
dataframe2:
dput(Large_Intestine_ALL_MUTANT[1:10, 1:6])
structure(list(SW948_LARGE_INTESTINE = c(30.3, 0.86, 117.62,
6.54, 12.54, 0.01, 0.01, 49.54, 44.32, 24.88), CCK81_LARGE_INTESTINE = c(33.6,
0.29, 117.17, 5.41, 15.02, 0.09, 0.06, 112.3, 51.45, 15.18),
RKO_LARGE_INTESTINE = c(11.89, 0.02, 153.59, 1.43, 12.71,
0, 0.13, 56.71, 15.72, 21.12), HCT116_LARGE_INTESTINE = c(26.62,
0, 108.14, 5.4, 23.62, 0, 0.04, 62.43, 52.36, 27.08), T84_LARGE_INTESTINE = c(56.72,
0.31, 75.88, 5.18, 16.03, 0.01, 0.04, 111.02, 41.39, 18.14
), CW2_LARGE_INTESTINE = c(36.58, 0.32, 44.1, 3.44, 7.57,
0.12, 0.1, 37.96, 46.32, 20.36)), row.names = c("ENSG00000000003.10",
"ENSG00000000005.5", "ENSG00000000419.8", "ENSG00000000457.9",
"ENSG00000000460.12", "ENSG00000000938.8", "ENSG00000000971.11",
"ENSG00000001036.9", "ENSG00000001084.6", "ENSG00000001167.10"
), class = "data.frame")
Thanks for your help.
I am very new to R and would like to create a filled contour plot in R to show oxygen concentration (z) changes in water column (y) over time (x). Bellow is how I made the initial plot using filled.contour() function. My questions are:
1- How to change the coloration of the plot? I like it to be between dark blue (high oxygen) to red (low oxygen).
2- As depth appears on Y axis, how can I make it start from 22 m (maximum depth of the lake) instead of 0 m to make plot easier to understand?
Thanks you
mat <- structure(c(9.76, 11.25, 11.15, 8.56, 8.94, 6.51, 8.30, 9.16,
6.75, 6.16, 6.48, 7.18, 7.17, 5.34, 5.03, 9.78, 11.52, 11.22, 7.39,
8.97, 6.48, 8.28, 9.40, 6.69, 6.11, 6.34, 7.06, 6.45, 5.05, 4.51, 9.72,
11.57, 11.21, 8.35, 8.96, 6.47, 8.21, 9.37, 6.60, 6.08, 6.31, 6.90,
5.86, 4.51, 3.56, 10.16, 11.53, 9.75, 8.53, 8.96, 6.44, 8.12, 9.37,
6.48, 6.04, 6.25, 6.81, 5.62, 4.19, 3.21, 10.48, 9.70, 8.78, 7.89,
9.00, 6.43, 8.08, 8.41, 6.33, 6.02, 6.22, 6.68, 5.51, 3.73, 2.88, 8.95,
8.01, 7.48, 7.42, 7.89, 6.41, 7.95, 7.80, 6.13, 5.92, 6.20, 6.52, 4.89,
3.31, 2.27, 7.41, 5.06, 4.10, 4.45, 3.25, 6.40, 7.71, 7.46, 5.97, 5.90,
6.17, 6.15, 4.31, 2.94, 1.72, 5.71, 3.86, 1.27, 1.97, 0.61, 6.38, 5.94,
7.43, 5.89, 5.87, 6.15, 5.81, 3.70, 2.70, 1.26, 5.02, 3.30, 0.80, 0.27,
0.48, 6.34, 4.89, 7.10, 5.87, 5.87, 6.12, 5.52, 3.34, 2.23, 0.58, 3.90,
3.02, 0.43, 0.18, 0.41, 5.92, 2.43, 6.97, 5.91, 5.92, 6.10, 5.01, 3.01,
0.32, 0.18, 3.46, 2.51, 0.24, 0.19, 0.38, 0.51, 0.19, 6.03, 5.92, 5.93,
6.09, 4.79, 1.86, 0.26, 0.10, 2.87, 1.35, 0.18, 0.14, 0.36, 0.23, 0.03,
5.52, 5.90, 5.97, 6.08, 4.46, 0.72, 0.20, 0.09, 1.67, 0.72, 0.12, 0.10,
0.34, 0.14, 0.00, 1.64, 5.72, 5.27, 6.06, 4.05, 0.52, 0.15, 0.07, 0.82,
0.59, 0.09, 0.08, 0.33, 0.13, 0.00, 0.41, 5.66, 5.86, 6.05, 0.90, 0.44,
0.14, 0.06, 0.48, 0.43, 0.07, 0.07, 0.32, 0.11, 0.00, 0.25, 2.47, 5.85,
6.03, 0.49, 0.39, 0.13, 0.05, 0.38, 0.37, 0.07, 0.07, 0.33, 0.10, 0.00,
0.19, 0.14, 5.62, 6.02, 0.35, 0.38, 0.12, 0.05, 0.28, 0.34, 0.07, 0.06,
0.33, 0.09, 0.00, 0.16, 0.08, 4.11, 6.02, 0.28, 0.36, 0.11, 0.04, 0.30,
0.33, 0.09, 0.07, 0.32, 0.09, 0.00, 0.15, 0.06, 0.72, 6.02, 0.24, 0.35,
0.10, 0.03, 0.31, 0.31, 0.11, 0.08, 0.31, 0.08, 0.00, 0.14, 0.04, 0.10,
6.01, 0.21, 0.34, 0.09, 0.02, 0.38, 0.31, 0.09, 0.07, 0.30, 0.08, 0.00,
0.14, 0.03, 0.06, 6.01, 0.22, 0.33, 0.07, 0.02, 0.44, 0.30, 0.07, 0.05,
0.29, 0.07, 0.00, 0.13, 0.02, 0.04, 6.01, 0.26, 0.31, 0.05, 0.02, 0.41,
0.30, 0.04, 0.04, 0.28, 0.06, 0.00, 0.14, 0.01, 0.02, 6.02, 0.27, 0.31,
0.05, 0.02, 0.37, 0.29, 0.04, 0.03, 0.28, 0.06, 0.00, 0.14, 0.00, 0.01,
6.02, 0.26, 0.31, 0.05, 0.02), .Dim = c(15L, 23L), .Dimnames =
list(c("06/25/2015", "07/08/2015","07/22/2015", "08/04/2015",
"08/18/2015", "09/01/2015", "09/15/2015", "09/30/2015","10/14/2015",
"10/26/2015", "11/12/2015", "01/06/2016", "01/20/2016", "02/04/2016",
"02/16/2016"), c("0.5", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22")))
dates <- as.Date(dimnames(mat)[[1]], "%m/%d/%Y")
depths <- as.numeric(dimnames(mat)[[2]])
filled.contour(x=dates, y=depths, z=mat, xlab="Date", ylab="Depth")
I couldn't figure out what you were talking about regarding starting at 22, but this will get you part of the way there.
cols = rev(colorRampPalette(c('darkred','red','blue','lightblue'))(24))
filled.contour(x=dates, y=depths, z=mat, xlab="Date", ylab="Depth",
col = cols
)
By the way, if I reversed the coloring then just remove the rev() statement.