Home > OS >  Iterating a calculation over rows
Iterating a calculation over rows

Time:08-09

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.

CodePudding user response:

rowSums() function perform the sum of the columns for every row. So in the code below you are multiplying element-wise the i-th column of the test[,4:11] to the i-th element of Nangle and then doing the aggregated sum per row.

test$N = rowSums(test[,4:11]*Nangle)

This returns an array of 155 elements, every element corresponds to the related row in test and it adds the array as a new column 'N'.

CodePudding user response:

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)
  • Related