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
Related
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 have digitized published survival curves and got the following (Survival,time) points
df<-structure(list(Survival = c(1, 1, 1, 1, 1, 1, 0.99, 0.99, 0.99,
0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99,
0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.98, 0.98, 0.98, 0.98,
0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98,
0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98,
0.98, 0.98, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97,
0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97,
0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97,
0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97,
0.97, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.95, 0.95, 0.95, 0.95, 0.95,
0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95,
0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95,
0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94, 0.94,
0.94, 0.94, 0.94, 0.94, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92,
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.91, 0.91, 0.91, 0.91, 0.91,
0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91,
0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91,
0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91,
0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89,
0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89,
0.89, 0.89, 0.89, 0.89, 0.89), time = c(0, 0.34, 0.55, 0.82,
0.87, 0.87, 0.87, 0.87, 0.87, 1.02, 1.02, 1.24, 1.45, 1.61, 1.74,
1.83, 1.86, 1.87, 1.87, 1.87, 1.88, 1.96, 2.16, 2.37, 2.75, 3.01,
3.48, 4.27, 4.5, 4.68, 4.89, 5.19, 5.58, 6.43, 6.75, 7.11, 7.55,
7.99, 8.41, 8.82, 9.26, 9.72, 10.09, 10.41, 10.68, 10.99, 11.29,
11.58, 11.85, 12.2, 12.47, 12.69, 13.03, 13.31, 13.72, 14.18,
14.48, 14.79, 15.26, 15.83, 16.08, 16.55, 16.92, 17.74, 18.54,
19.24, 19.54, 19.92, 21.14, 21.51, 21.89, 22.98, 23.29, 23.55,
23.78, 24.1, 24.38, 24.79, 25.09, 25.42, 25.78, 27.09, 27.66,
28.18, 28.57, 28.97, 29.36, 29.76, 30.15, 30.54, 30.94, 31.33,
31.73, 32.12, 32.64, 33.19, 33.56, 33.8, 34.05, 34.22, 34.5,
34.79, 35.1, 35.61, 36.05, 36.47, 36.87, 37.26, 37.65, 38.05,
38.44, 38.84, 39.23, 39.63, 40.02, 40.42, 40.81, 41.21, 41.6,
41.99, 42.51, 43.1, 43.41, 43.98, 44.91, 45.55, 46.53, 47.44,
47.83, 48.17, 49.03, 49.65, 50.45, 50.84, 51.22, 51.61, 52.4,
52.85, 53.24, 53.62, 54.62, 55.11, 55.46, 56, 56.61, 57.39, 57.75,
58.15, 58.47, 58.77, 59.01, 59.57, 59.94, 60.44, 60.95, 61.35,
61.74, 62.14, 62.53, 62.93, 63.32, 63.71, 64.11, 64.62, 65.3,
65.61, 65.96, 66.21, 66.71, 67.08, 67.6, 68.04, 68.45, 68.85,
69.24, 69.64, 70.03, 70.43, 70.82, 71.22, 71.61, 72.01, 72.4,
72.79, 73.19, 73.58, 73.98, 74.37, 74.79, 75.23, 75.68, 76.13,
76.41, 76.65, 76.88, 77.12, 77.52, 77.88, 78.3, 78.8, 79.22,
79.64, 79.95, 80.94, 81.32, 81.66, 82.5, 82.86, 83.24, 83.58,
84.01, 84.27, 84.63, 84.94, 85.28, 86.25, 86.59, 86.91, 88.39,
88.73, 89.3, 89.59, 90.12, 90.39, 90.67, 91.18, 91.7, 92.14,
92.55, 92.95, 93.34, 93.74, 94.13, 94.53, 94.92, 95.31, 95.71,
96.1, 96.5, 96.89, 97.29, 97.68, 98.08, 98.47, 98.87, 99.26,
99.66, 100.05, 100.44, 100.84, 101.23, 101.63, 102.02, 102.42,
102.81, 103.21, 103.6, 104, 104.39, 104.78, 105.18, 105.57, 105.97,
106.36, 106.76, 107.15, 107.55, 107.94, 108.34, 108.73, 109.13,
109.52, 109.91, 110.31, 110.7, 111.12, 111.56, 112.27, 112.64,
113.1, 113.59, 114.1, 114.46, 114.8, 115.24, 115.55, 116, 116.34,
117.03, 117.85, 118.24, 118.58, 118.89, 120.23, 120.55, 120.85,
121.92, 122.4, 122.89, 123.33, 123.75, 124.14, 124.54, 124.93,
125.33, 125.72, 126.11, 126.51, 126.9, 127.3, 127.69, 128.09,
128.48, 128.88, 129.27, 129.67, 130.06, 130.45, 130.85, 131.24,
131.64, 132.03, 132.43, 132.82, 133.22, 133.61, 134.01, 134.4,
134.8, 135.19, 135.58, 135.98, 136.37, 136.77, 137.16, 137.56,
137.95, 138.35, 138.74, 139.14, 139.53, 139.92, 140.34, 140.78,
141.49, 141.86, 142.29, 142.73, 143.3, 143.81, 144.27, 144.73,
145.23, 145.66, 146.01, 146.33, 146.92, 147.74, 148.26, 148.66,
149.21, 149.93, 150.33, 150.87, 151.38, 151.78, 152.17, 152.57,
152.96, 153.36, 153.75, 154.14, 154.54, 154.93, 155.33, 155.72,
156.12, 156.51, 156.91, 157.3, 157.81, 158.49, 158.82, 159.33,
159.8, 160.19, 160.74, 161.25, 161.76, 162.44, 162.76, 163.1,
163.35, 163.98, 164.38, 164.9, 165.25, 165.57, 166.11, 167.09,
167.48, 167.9, 168.34, 168.75, 169.15, 169.54, 169.94, 170.35,
170.79, 171.51, 171.88, 172.31, 172.74, 173.32, 173.82, 174.26,
174.68, 175.07, 175.47, 175.86, 176.26, 176.65, 177.04, 177.44,
177.83, 178.23, 178.62, 179.02, 179.41, 179.81, 180.2, 180.6,
180.99, 181.38, 181.78, 182.17, 182.57, 182.96, 183.36, 183.75,
184.15, 184.54, 184.94, 185.33, 185.73, 186.12, 186.51, 186.91,
187.3, 187.7, 188.09, 188.49, 188.88, 189.28, 189.67, 190.07,
190.46, 190.85, 191.25, 191.64, 192.04, 192.43, 192.83, 193.22,
193.62, 194.01, 194.41, 194.8, 195.2, 195.59, 195.98, 196.38,
196.77, 197.17, 197.56, 197.96, 198.37, 198.81, 199.62, 199.91,
200.33, 200.85, 201.34, 201.84, 202.28, 202.7, 203.09, 203.48,
203.88, 204.27, 204.67, 205.06, 205.46, 205.85, 206.25, 206.64,
207.04, 207.43, 207.83, 208.22, 208.61, 209.01, 209.4, 209.8,
210.19, 210.59, 210.98, 211.38, 211.77, 212.17, 212.56, 212.95,
213.37, 213.81, 214.33, 214.73, 215.63, 216.28, 216.79, 217.3,
217.69, 218.09, 218.48, 218.88, 219.27, 219.67, 220.06, 220.46,
220.85, 221.24, 221.64, 222.03, 222.43, 222.82, 223.22, 223.61,
224.01, 224.4, 224.8, 225.19, 225.59, 225.98, 226.37, 226.77,
227.16, 227.56, 227.95, 228.35, 228.74, 229.14, 229.53, 229.93,
230.32, 230.71, 231.11, 231.5, 231.9, 232.29, 232.81, 233.34,
233.77, 234.34, 235.23, 235.73, 236.24, 236.64, 237.03, 237.43,
237.82, 238.22, 238.61, 239.4, 239.77, 240.07, 241.47, 242.05,
242.56, 242.95, 243.35, 243.74, 244.14, 244.53, 244.93, 245.32,
245.72, 246.11, 246.51, 246.9, 247.29, 247.69, 248.08, 248.48,
248.87, 249.27, 249.66, 250.06, 250.45, 250.85, 251.24, 251.63,
252.03, 252.42, 253.4, 253.99, 254.7, 255.15, 255.56, 256.09,
256.54, 256.87, 257.19, 258.14, 258.48, 258.8, 259.18, 260.18,
260.56, 261.05, 261.49, 261.9, 262.3, 262.69, 263.09, 263.48,
264.14, 264.98, 265.49, 265.87, 266.22, 267.66, 268.15, 268.6,
269.01, 269.41, 269.8, 270.19, 270.59, 270.98, 271.4, 271.84,
272.36, 272.76, 273.66, 274.31, 274.82, 275.33, 275.72, 276.12,
276.63, 277.32, 277.67, 277.91, 278.28, 278.54, 278.84, 279.16,
279.56, 280, 280.73, 281.48, 282.04, 282.61, 283.31, 284.14,
284.32, 284.44, 284.64, 284.88, 285.15, 285.45, 285.79, 286.17,
286.72, 287.15, 287.48, 287.82, 288.35, 289.08, 290.04, 290.32,
290.82, 291.32, 291.7, 292.2, 292.71, 293.42, 293.94, 294.56,
295.29, 295.61, 296.27, 297.13, 297.45, 297.79, 298.22, 298.54,
298.88, 299.41, 299.82, 300.22, 300.61, 301.01, 301.4, 301.8,
302.19, 302.58, 302.98, 303.37, 303.77, 304.16, 304.56, 305.27,
306.03, 306.39, 306.86, 307.2, 307.62, 307.95, 308.49, 308.9,
309.3, 309.69, 310.08, 310.48, 310.87, 311.27, 311.66, 312.06,
312.45, 312.85, 313.24, 313.64, 314.03, 314.43, 314.82, 315.21,
315.61, 316, 316.4, 316.79, 317.19, 317.58, 317.98, 318.37, 318.77,
319.16, 319.55, 319.95, 320.34, 320.74, 321.13, 321.53, 321.92,
322.32, 323.03, 324.13, 324.49, 325.03, 325.77, 326.1, 326.57,
326.9, 327.43, 327.85, 328.24, 328.64, 329.03, 329.43, 329.82,
330.22, 330.61, 331, 331.4, 331.79, 332.19, 332.58, 332.98, 333.37,
333.79, 334.23, 334.7, 335.03, 335.55, 335.89, 336.36, 336.71,
337.02, 337.47, 338, 338.51, 338.9, 339.3, 339.69, 340.09, 340.48,
340.88, 341.27, 341.67, 342.06, 342.45, 342.85, 343.24, 343.64,
344.03, 344.43, 344.82, 345.22, 345.61, 346.01, 346.4, 346.8,
347.19, 347.58, 347.98, 348.49, 348.88, 349.24, 349.55, 349.89,
350.32, 350.65, 350.98, 351.51, 352.25, 353.19, 353.49, 353.83,
354.27, 354.6, 354.93, 355.46, 355.88, 356.27, 356.67, 357.06,
357.46, 357.85, 358.25, 358.64, 359.3, 360.16, 360.78, 361.24,
361.79, 362.27, 362.83, 363.19, 363.79)), class = "data.frame", row.names = c(NA,
-848L))
From which i get
plot(df$time, df$Survival, ylim=c(0,1), type="s", lwd=2, bty="n", xlim=c(0,400))
How can I calculate the smoothed instantaneous hazard function from this dataframe? I dont have access to number at risk.
The theoretical instantaneous hazard is the time derivative of the cumulative Hazard function which is just 1-Survival. I disagree that the "number at risk" should be necessary in this instance, because you have already created an empirical Survival function. Looking at the Survival curve you would expect a correct estimate to have an early peak and then taper off. So see whether this estimator suffices:
#Pseudo-code
IHF <- { ( S(x+1) - S(x) ) /S(x) }/ # the S(x) in denominator replaces n-at-risk
(t(x+1)-t(x)
# R-code
IHF <- function( S, t) { (-diff(S)/head(S, -1))/diff(t) }
plot( head(df$time,-1) , IHF(df$Survival, df$time) )
Well wasn't entirely satisfactory. You only have 11 instances where there is a decrement, so I guess I can see why you want a smoothed estimator. Lets see if loess can suffice. Nope. Couldn't get a sufficient bandwidth. The muhaz package is designed for this. I'm not sure if it's the best for it, but it's the one I'm most familiar with. It designed for censored data but the default for its censoring status is to assume no cases are censored, so yo can just provide times and it will take the survival into account:
fit2 <- muhaz(head(df$time,-1)[ as.logical(diff(df$Survival)) ], maxtime=360)
plot(fit2)
I wasn't entirely happy with that result, being concerned about the late peak. So I tried anotehr package:
install.packages("bshazard"
library(bshazard
times=head(df$time,-1)[ as.logical(diff(df$Survival)) ]
times
#[1] 0.87 3.48 13.72 33.80 75.68 90.39 145.66 200.85 272.76 295.29 352.25
fit<-bshazard( Surv(times,rep(1, length(times))) ~ 1, data=df)
I line that one a bit better. (Which is a bit ironic since I'm the maintainer of pkg:muhaz, but I'm just a caretaker.)
x = c(0.80, 1.23, -0.13, -0.65, 0.53, -0.10, -0.96, 1.63, 2.79, 2.02, -1.03, -0.86,
0.58, 0.59, 0.60, -1.77, -0.77, -0.73, -0.43, 2.60, -0.81, -2.81, -2.13, 1.66,
1.54, -0.15, -0.31, 0.09, 2.47, 0.24, -0.75, 2.09, 0.46, -0.80, -0.50, 2.58,
0.80, 0.39, 0.82, -0.58, -1.09, -0.29, -1.26, -1.72, 1.54, 1.06, 1.21, 0.15,
-0.57, -0.32, -1.44, -1.56)
z = acf(x, lag.max = 4, type = c("covariance"))
z
I am stuck in Number 3 question of this image:
In Correlation plot, How can I keep the labels in the same order as in datafile, without rearranging?
This is the scripts I have been using;
library(corrplot)
library(RColorBrewer)
library(Hmisc)
CORR <- df
M <- cor(CORE)
pM <- rcorr(M)
corrplot(pM$r,
type="upper",
order="hclust",
tl.cex = 0.5,
col=colorRampPalette(c("blue4", "white", "firebrick1"))(100),
p.mat = pM$P,
sig.level = 0.05,
insig = "blank"
)
sample data
df <- structure(list(Cytosol_1 = c(2.8, 0.31, 1.21, 1.84, 0.93, 2.71, 2.03, 0.93, 0.89, 0.4, 0.32, 1.8, 1.16, 0.39, 1.16, 0.76, 1.17, 0.95, 0.58, 2.68, 0.88, 0.59, 1.49, 0.48, 0.51, 1.04, 0.89, 3.48, 1.47), Cytosol_2 = c(1.61, 0.22, 1.42, 1.97, 0.88, 1.46, 1.43, 0.74, 0.72, 0.43, 0.51, 2.07, 1.29, 0.71, 0.92, 0.57, 1.9, 0.84, 0.4, 2.72, 1.08, 0.96, 1.75, 0.24, 0.76, 0.99, 2.35, 2.06, 1.24 ), Cytosol_3 = c(1.76, 0.27, 0.77, 1.23, 0.93, 1.43, 0.7, 0.44, 0.58, 0.47, 0.57, 0.85, 0.79, 0.75, 0.95, 0.85, 1.49, 1.19, 0.72, 1.92, 1.11, 1.18, 1.03, 0.75, 0.58, 0.7, 0.79, 1.64, 1.14), Cytosol_4 = c(1.41, 0.98, 0.73, 2.31, 1.07, 1.1, 1.31, 0.66, 0.69, 0.51, 0.53, 1.3, 1.37, 1.55, 0.99, 1.27, 1.22, 0.89, 1.21, 1.56, 1.14, 0.7, 0.3, 0.63, 1.73, 1.49, 0.92, 1.8, 1.7), Cytosol_5 = c(2.36, 0.25, 1.43, 2.76, 0.91, 2.88, 2.73, 0.79, 0.71, 0.15, 0.92, 1.94, 1.12, 0.64, 2.07, 0.68, 1.51, 0.66, 0.51, 1.91, 1.61, 0.68, 0.73, 0.7, 0.94, 1.24, 2.45, 3.12, 1.58), Ribosome_6 = c(1.52, 1.09, 1.58, 1.29, 0.92, 1.12, 0.8, 0.51, 0.87, 0.58, 0.42, 0.72, 1.39, 1.14, 1.87, 1.11, 1.11, 1.07, 0.84, 1.11, 1.17, 0.45, 0.49, 0.59, 0.89, 0.79, 0.67, 1.66, 1.86), Ribosome_7 = c(4.16, 0.98, 1.79, 2.21, 1.21, 1.31, 1.01, 0.02, 0.82, 0.51, 0.81, 0.73, 2.34, 1.04, 1.99, 0.92, 2.2, 0.74, 0.25, 1.71, 1.43, 0.67, 1.19, 0.49, 1.5, 1.14, 0.92, 3.67, 2.68), Ribosome_8 = c(2.02, 0.95, 1.79, 1.47, 0.87, 1.88, 0.97, 0.51, 0.77, 0.66, 0.54, 1, 1.54, 0.92, 1.73, 1.32, 1.89, 0.97, 0.87, 1.26, 1.1, 0.61, 0.49, 0.57, 0.91, 0.76, 0.86, 3.37, 3.1), Ribosome_9 = c(2.67, 0.45, 1.45, 1.41, 0.56, 1.93, 1.29, 0.44, 0.58, 0.38, 0.3, 1.15, 1.5, 0.67, 1.38, 0.72, 1.71, 0.74, 0.5, 2.2, 1.36, 0.74, 1.06, 0.54, 0.72, 0.83, 1.27, 2.55, 1.4), Ribosome_10 = c(2.19, 0.55, 1.39, 1.56, 0.62, 1.67, 1.31, 0.47, 0.46, 0.35, 0.4, 1.02, 1.32, 0.7, 0.96, 0.6, 1.63, 0.94, 0.38, 1.6, 0.92, 0.71, 0.81, 0.56, 0.77, 0.73, 1.14, 2.42, 1.11 ), ER_17 = c(1.41, 0.29, 0.32, 0.76, 0.7, 2.75, 2.78, 0.8, 0.96, 0.28, 0.82, 2.15, 1.19, 0.55, 0.78, 0.97, 1.42, 1.22, 0.92, 1.84, 0.6, 0.69, 0.43, 0.39, 0.38, 0.48, 0.36, 2.17, 1.03), ER_18 = c(1.02, 0.43, 0.98, 1.91, 0.88, 3.19, 1.05, 1.65, 0.53, 1.08, 0.39, 1.1, 0.36, 0.56, 0.58, 1.13, 1.25, 1.03, 0.79, 1.67, 0.56, 0.84, 1.17, 1.05, 0.18, 0.69, 0.09, 1.58, 0.47), ER_19 = c(0.58, 0.72, 0.58, 1.42, 0.52, 2.18, 0.95, 0.32, 1.44, 0.86, 0.24, 0.8, 0.62, 0.34, 0, 1.29, 1.29, 0.84, 1.43, 4.48, 1.82, 0.97, 0.83, 1.25, 0.29, 0.03, 1.48, 1.72, 1.89), Extracellular_20 = c(2.77, 0.43, 2.23, 1.86, 0.51, 2.96, 1.41, 0.91, 0.61, 0.64, 0.84, 2.03, 1.34, 0.69, 0.82, 0.5, 1.18, 0.77, 0.59, 1.65, 1, 0.74, 1, 0.55, 1.38, 1.38, 1.82, 1.58, 1.02), Extracellular_21 = c(3.4, 0.67, 1.91, 1.76, 0.77, 1.65, 1.04, 0.48, 0.53, 0.34, 0.48, 1.24, 1.49, 1.07, 1.24, 0.81, 1.4, 0.85, 0.46, 1.52, 0.9, 0.81, 0.6, 0.57, 1.32, 1.3, 2.22, 1.29, 0.98), Extracellular_22 = c(0.52, 0.01, 0.33, 2.25, 1.05, 2.63, 2.5, 0.99, 0.53, 1.21, 1.43, 3.2, 0.32, 0.2, 0.23, 0.52, 1.07, 0.51, 0.55, 2.58, 1.5, 1.19, 2.16, 0.81, 0.02, 0.1, 0.2, 2.38, 1.36), Extracellular_23 = c(0.16, 0, 0, 1.8, 0.74, 1.45, 2.6, 0.8, 1.68, 1.63, 2.41, 1.46, 0.52, 0.67, 0.05, 1.12, 0.78, 0.62, 0.56, 1.74, 1.03, 1.74, 1.14, 1.33, 0, 0, 0, 0.96, 1.46)), class = "data.frame", row.names = c(NA, -29L))
The corrplot function has an "order" argument, which allows you to specify how the rows and columns of the plot are arranged. Setting order = 'original' preserves the ordering in the source data frame:
corrplot(pM$r,
type="upper",
order="original",
tl.cex = 0.5,
col=colorRampPalette(c("blue4", "white", "firebrick1"))(100),
p.mat = pM$P,
sig.level = 0.05,
insig = "blank"
)