I have a dataframe J and I am currently using a code where I am performing some simple operations on this dataframe. The current code is at the end.
What I want to get
There is another dataframe named CFD_L2. This data frame has 3 columns: xpos, ypos, and velocity. Now in the J dataframe, I want to update the fishspeed using the velocity (from the CFD_L2 dataframe based on the xpos and ypos) by adding it to the current fish speed (which is calculated using the distance formula).
So the algorithm steps would go something like this.
- The code will work as it is right now.
- Then the code will go through the J dataframe row by row and check the X.wcentroid..cm. and Y.wcentroid..cm. position and then look for the corresponding xpos and ypos in the CFD_L2 dataframe (it will have to do interpolation in the CFD_L2 to find those exact xpos and ypos as of X.wcentroid..cm. and Y.wcentroid..cm.) and pick the velocity from there and add it to the already calculated fishspeed in the J dataframe.
If someone could help me in building this piece of missing code, I will be very grateful. Thank you in advance. :)
#The J dataframe with fish data
J <- read.csv("D:/MSCA-RIBES/Experiments_2021/R/Fish3.csv")
#The dataframe from where I want to extract velocity values and add them to the J dataframe
CFD_L2 <- read.csv(paste0("D:/MSCA-RIBES/Ansys Fluent/L2 Model/2/plane_L2.csv"))
J$fishspeed[1] <- 0 #assigning 0 as the starting fish speed
J$distance <- NA #create the distance column
for (j in 2:nrow(J)) {
#calculating distance from distance formula (both in x and y)
J$distance[j] <- abs(J$X.wcentroid..cm.[j]-J$X.wcentroid..cm.[j-1])
#calculating fish speed in x and y
J$fishspeed[j] <- J$distance[j]/(0.02)
#assigning positive and negative signs to the velocity
J$fishspeed[j] <- ifelse(J$X.wcentroid..cm.[j]-J$X.wcentroid..cm.[j-1] < 0,J$fishspeed[j],-J$fishspeed[j])
}
The MRE for the J dataframe is:
structure(list(frame = 0:9, ACCELERATION.pcentroid..cm.s2. = c(0,
629.05127, 484.44827, 134.24866, 168.38608, 392.61456, 534.75482,
41.96627, 69.14757, 263.45416), ACCELERATION.wcentroid..cm.s2. = c(0,
153.44151, 105.59528, 202.80286, 118.33042, 131.19977, 297.22171,
77.18677, 61.05884, 230.39838), ANGLE = c(3.11237, 3.08248, 3.07114,
-0.10611, -3.11957, 3.14106, -3.10649, -0.083, -0.10303, -0.03427
), ANGULAR_A.centroid = c(0, -202.77382, 3.05533, -7495.91406,
15768.4209, -8005.69336, -236.58276, 7894.97559, -23338.25391,
15657.55469), ANGULAR_V.centroid = c(0, -4.05548, -3.99437, -153.91264,
161.45576, 1.3419, -3.38975, 154.50975, -312.25534, 0.89574),
AX = c(0, -2041.00696, -2051.98926, 2269.85352, -2141.18726,
-2167.49097, -1622.0061, 2030.82153, 2261.80957, 2080.86084
), AY = c(0, 35.52997, 171.27061, -312.76627, -215.38239,
-268.79153, -71.59282, -323.54047, -368.00244, -342.0845),
BORDER_DISTANCE.pcentroid..cm. = c(0.55631, 0.53378, 0.49995,
0.43229, 0.40511, 0.42238, 0.3941, 0.3742, 0.33689, 0.29436
), MIDLINE_OFFSET = c(0.08791, 0.06723, -0.00448, -0.01109,
0.03456, 0.08484, 0.04347, -0.05106, -0.02282, -0.04411),
SPEED.wcentroid..cm.s. = c(0, 3.06883, 4.4169, 3.33479, 1.50971,
2.36096, 3.75872, 2.57346, 2.4795, 3.60654), SPEED.smooth.wcentroid..cm.s. = c(0,
3.06883, 4.4169, 3.33479, 1.50971, 2.36096, 3.75872, 2.57346,
2.4795, 3.60654), SPEED.pcentroid..cm.s. = c(0, 12.58103,
12.6677, 1.91822, 1.10765, 6.40553, 8.74503, 4.52055, 1.54492,
4.30662), SPEED..cm.s. = c(0, 40.82632, 38.38213, 48.67908,
43.71307, 44.80465, 34.20591, 44.49779, 47.65644, 42.92842
), VX..cm.s. = c(0, -40.82014, -38.12928, 48.57462, -43.69948,
-44.57827, -34.06067, 44.18293, 47.29947, 42.68103), VY..cm.s. = c(0,
0.7106, 4.39839, -3.1874, -1.0899, -4.49826, -3.14883, -5.28418,
-5.82202, -4.602), X.wcentroid..cm. = c(24.36773, 24.42594,
24.48949, 24.47197, 24.4474, 24.41499, 24.48632, 24.52759,
24.54886, 24.48055), X..cm. = c(23.48358, 23.55132, 23.66335,
25.46098, 23.59798, 23.55584, 23.73378, 25.36998, 25.47358,
25.40248), speed = c(0, -3.387, -5.6015, -89.8815, 93.15,
2.107, -8.897, -81.81, -5.18, 3.555), Y.wcentroid..cm. = c(29.98473,
30.00418, 30.06554, 30.1299, 30.14745, 30.11311, 30.13684,
30.1676, 30.2124, 30.23556), Y..cm. = c(29.97619, 29.99894,
30, 30, 30, 30, 30, 30, 30, 30), frame.1 = 0:9, midline_length = c(78.90591,
79.62206, 78.76032, 72.85874, 74.39832, 78.09255, 74.34315,
69.12302, 73.73874, 75.47749), midline_x = c(23.15566, 23.22088,
23.33655, 25.76223, 23.28882, 23.23115, 23.42478, 25.65648,
25.7786, 25.71614), midline_y = c(29.98568, 30.01886, 30.11412,
29.96982, 30.09885, 30.05787, 30.03924, 30.00842, 30.02002,
30.11098), missing = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), normalized_midline = c(0.1907, 0.08918, -0.12857, Inf,
Inf, 0.1425, Inf, Inf, Inf, 0.00509), num_pixels = c(1376L,
1304L, 1261L, 1165L, 1214L, 1240L, 1198L, 1020L, 917L, 884L
), segment_length = c(0.10935, 0.11035, 0.10918, 0.10098,
0.1031, 0.10823, 0.10307, 0.0958, 0.1022, 0.1046), time = c(0,
0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18), timestamp = c(0L,
20000L, 40000L, 60000L, 80000L, 100000L, 120000L, 140000L,
160000L, 180000L)), row.names = c(NA, 10L), class = "data.frame")
The MRE for CFD_L2 is:
structure(list(ï..xpos = c(17.3991, 17.9987, 18.5983, 19.1979,
19.7975, 20.3971, 20.9967, 21.5963, 22.1959, 22.7955, 23.3951,
23.9947, 24.5943, 25.1939, 25.7935, 26.3931, 26.9927, 27.5923,
28.1919, 28.7915, 29.3911, 30), ypos = c(30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30), velocity = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0)), row.names = 3600:3621, class = "data.frame")
> CFD <- dput(CFD_L2[3000:3621,])
structure(list(ï..xpos = c(24.5943, 25.1939, 25.7935, 26.3931,
26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30, 0, 0.6104, 1.21,
1.8096, 2.4092, 3.0088, 3.6084, 4.208, 4.8076, 5.4072, 6.0068,
6.6064, 7.206, 7.8056, 8.4052, 9.0048, 9.6044, 10.204, 10.8036,
11.4032, 12.0028, 12.6024, 13.2019, 13.8015, 14.4011, 15.0007,
15.6003, 16.1999, 16.7995, 17.3991, 17.9987, 18.5983, 19.1979,
19.7975, 20.3971, 20.9967, 21.5963, 22.1959, 22.7955, 23.3951,
23.9947, 24.5943, 25.1939, 25.7935, 26.3931, 26.9927, 27.5923,
28.1919, 28.7915, 29.3911, 30, 0, 0.6104, 1.21, 1.8096, 2.4092,
3.0088, 3.6084, 4.208, 4.8076, 5.4072, 6.0068, 6.6064, 7.206,
7.8056, 8.4052, 9.0048, 9.6044, 10.204, 10.8036, 11.4032, 12.0028,
12.6024, 13.2019, 13.8015, 14.4011, 15.0007, 15.6003, 16.1999,
16.7995, 17.3991, 17.9987, 18.5983, 19.1979, 19.7975, 20.3971,
20.9967, 21.5963, 22.1959, 22.7955, 23.3951, 23.9947, 24.5943,
25.1939, 25.7935, 26.3931, 26.9927, 27.5923, 28.1919, 28.7915,
29.3911, 30, 0, 0.6104, 1.21, 1.8096, 2.4092, 3.0088, 3.6084,
4.208, 4.8076, 5.4072, 6.0068, 6.6064, 7.206, 7.8056, 8.4052,
9.0048, 9.6044, 10.204, 10.8036, 11.4032, 12.0028, 12.6024, 13.2019,
13.8015, 14.4011, 15.0007, 15.6003, 16.1999, 16.7995, 17.3991,
17.9987, 18.5983, 19.1979, 19.7975, 20.3971, 20.9967, 21.5963,
22.1959, 22.7955, 23.3951, 23.9947, 24.5943, 25.1939, 25.7935,
26.3931, 26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30, 0,
0.6104, 1.21, 1.8096, 2.4092, 3.0088, 3.6084, 4.208, 4.8076,
5.4072, 6.0068, 6.6064, 7.206, 7.8056, 8.4052, 9.0048, 9.6044,
10.204, 10.8036, 11.4032, 12.0028, 12.6024, 13.2019, 13.8015,
14.4011, 15.0007, 15.6003, 16.1999, 16.7995, 17.3991, 17.9987,
18.5983, 19.1979, 19.7975, 20.3971, 20.9967, 21.5963, 22.1959,
22.7955, 23.3951, 23.9947, 24.5943, 25.1939, 25.7935, 26.3931,
26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30, 0, 0.6104, 1.21,
1.8096, 2.4092, 3.0088, 3.6084, 4.208, 4.8076, 5.4072, 6.0068,
6.6064, 7.206, 7.8056, 8.4052, 9.0048, 9.6044, 10.204, 10.8036,
11.4032, 12.0028, 12.6024, 13.2019, 13.8015, 14.4011, 15.0007,
15.6003, 16.1999, 16.7995, 17.3991, 17.9987, 18.5983, 19.1979,
19.7975, 20.3971, 20.9967, 21.5963, 22.1959, 22.7955, 23.3951,
23.9947, 24.5943, 25.1939, 25.7935, 26.3931, 26.9927, 27.5923,
28.1919, 28.7915, 29.3911, 30, 0, 0.6104, 1.21, 1.8096, 2.4092,
3.0088, 3.6084, 4.208, 4.8076, 5.4072, 6.0068, 6.6064, 7.206,
7.8056, 8.4052, 9.0048, 9.6044, 10.204, 10.8036, 11.4032, 12.0028,
12.6024, 13.2019, 13.8015, 14.4011, 15.0007, 15.6003, 16.1999,
16.7995, 17.3991, 17.9987, 18.5983, 19.1979, 19.7975, 20.3971,
20.9967, 21.5963, 22.1959, 22.7955, 23.3951, 23.9947, 24.5943,
25.1939, 25.7935, 26.3931, 26.9927, 27.5923, 28.1919, 28.7915,
29.3911, 30, 0, 0.6104, 1.21, 1.8096, 2.4092, 3.0088, 3.6084,
4.208, 4.8076, 5.4072, 6.0068, 6.6064, 7.206, 7.8056, 8.4052,
9.0048, 9.6044, 10.204, 10.8036, 11.4032, 12.0028, 12.6024, 13.2019,
13.8015, 14.4011, 15.0007, 15.6003, 16.1999, 16.7995, 17.3991,
17.9987, 18.5983, 19.1979, 19.7975, 20.3971, 20.9967, 21.5963,
22.1959, 22.7955, 23.3951, 23.9947, 24.5943, 25.1939, 25.7935,
26.3931, 26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30, 0,
0.6104, 1.21, 1.8096, 2.4092, 3.0088, 3.6084, 4.208, 4.8076,
5.4072, 6.0068, 6.6064, 7.206, 7.8056, 8.4052, 9.0048, 9.6044,
10.204, 10.8036, 11.4032, 12.0028, 12.6024, 13.2019, 13.8015,
14.4011, 15.0007, 15.6003, 16.1999, 16.7995, 17.3991, 17.9987,
18.5983, 19.1979, 19.7975, 20.3971, 20.9967, 21.5963, 22.1959,
22.7955, 23.3951, 23.9947, 24.5943, 25.1939, 25.7935, 26.3931,
26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30, 0, 0.6104, 1.21,
1.8096, 2.4092, 3.0088, 3.6084, 4.208, 4.8076, 5.4072, 6.0068,
6.6064, 7.206, 7.8056, 8.4052, 9.0048, 9.6044, 10.204, 10.8036,
11.4032, 12.0028, 12.6024, 13.2019, 13.8015, 14.4011, 15.0007,
15.6003, 16.1999, 16.7995, 17.3991, 17.9987, 18.5983, 19.1979,
19.7975, 20.3971, 20.9967, 21.5963, 22.1959, 22.7955, 23.3951,
23.9947, 24.5943, 25.1939, 25.7935, 26.3931, 26.9927, 27.5923,
28.1919, 28.7915, 29.3911, 30, 0, 0.6104, 1.21, 1.8096, 2.4092,
3.0088, 3.6084, 4.208, 4.8076, 5.4072, 6.0068, 6.6064, 7.206,
7.8056, 8.4052, 9.0048, 9.6044, 10.204, 10.8036, 11.4032, 12.0028,
12.6024, 13.2019, 13.8015, 14.4011, 15.0007, 15.6003, 16.1999,
16.7995, 17.3991, 17.9987, 18.5983, 19.1979, 19.7975, 20.3971,
20.9967, 21.5963, 22.1959, 22.7955, 23.3951, 23.9947, 24.5943,
25.1939, 25.7935, 26.3931, 26.9927, 27.5923, 28.1919, 28.7915,
29.3911, 30, 0, 0.6104, 1.21, 1.8096, 2.4092, 3.0088, 3.6084,
4.208, 4.8076, 5.4072, 6.0068, 6.6064, 7.206, 7.8056, 8.4052,
9.0048, 9.6044, 10.204, 10.8036, 11.4032, 12.0028, 12.6024, 13.2019,
13.8015, 14.4011, 15.0007, 15.6003, 16.1999, 16.7995, 17.3991,
17.9987, 18.5983, 19.1979, 19.7975, 20.3971, 20.9967, 21.5963,
22.1959, 22.7955, 23.3951, 23.9947, 24.5943, 25.1939, 25.7935,
26.3931, 26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30, 0,
0.6104, 1.21, 1.8096, 2.4092, 3.0088, 3.6084, 4.208, 4.8076,
5.4072, 6.0068, 6.6064, 7.206, 7.8056, 8.4052, 9.0048, 9.6044,
10.204, 10.8036, 11.4032, 12.0028, 12.6024, 13.2019, 13.8015,
14.4011, 15.0007, 15.6003, 16.1999, 16.7995, 17.3991, 17.9987,
18.5983, 19.1979, 19.7975, 20.3971, 20.9967, 21.5963, 22.1959,
22.7955, 23.3951, 23.9947, 24.5943, 25.1939, 25.7935, 26.3931,
26.9927, 27.5923, 28.1919, 28.7915, 29.3911, 30), ypos = c(28.4519,
28.4519, 28.4519, 28.4519, 28.4519, 28.4519, 28.4519, 28.4519,
28.4519, 28.4519, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367, 29.0367,
29.0367, 29.0367, 29.0367, 29.0367, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306,
29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.6306, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032, 29.7032,
29.7032, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764,
29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764,
29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764,
29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764,
29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764,
29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764, 29.764,
29.764, 29.764, 29.764, 29.7641, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8149,
29.8149, 29.8149, 29.8149, 29.8149, 29.8149, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575, 29.8575,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931, 29.8931,
29.8931, 29.8931, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228, 29.9228,
29.9228, 29.9228, 29.9228, 29.9228, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476,
29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9476, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683, 29.9683,
29.9683, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856, 29.9856,
29.9856, 29.9856, 29.9856, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30), velocity = c(0.330644,
0.329754, 0.328881, 0.32803, 0.327197, 0.326397, 0.325606, 0.324901,
0.324086, 0.333277, 0.355185, 0.361662, 0.365557, 0.36612, 0.366069,
0.365769, 0.36523, 0.364538, 0.36373, 0.362818, 0.361821, 0.360753,
0.35963, 0.358459, 0.357247, 0.356, 0.354722, 0.353418, 0.352091,
0.350742, 0.349373, 0.347988, 0.34659, 0.345182, 0.343769, 0.342353,
0.340936, 0.33952, 0.338106, 0.336697, 0.335294, 0.3339, 0.332517,
0.331146, 0.329788, 0.328444, 0.327117, 0.325806, 0.324513, 0.323239,
0.321985, 0.320752, 0.319542, 0.318356, 0.317199, 0.316068, 0.314981,
0.313911, 0.312959, 0.311883, 0.322129, 0.355185, 0.363593, 0.368111,
0.367753, 0.367014, 0.366066, 0.364919, 0.363646, 0.36226, 0.360762,
0.359165, 0.357484, 0.35573, 0.353914, 0.352043, 0.350122, 0.348158,
0.346154, 0.344117, 0.34205, 0.339955, 0.337837, 0.335707, 0.333571,
0.331434, 0.329296, 0.327155, 0.325015, 0.32288, 0.320751, 0.318635,
0.316536, 0.314459, 0.312406, 0.310379, 0.308381, 0.306414, 0.304479,
0.302579, 0.300714, 0.298886, 0.297097, 0.295349, 0.293644, 0.291986,
0.290374, 0.28883, 0.287319, 0.28598, 0.284482, 0.297058, 0.355185,
0.363776, 0.368044, 0.367231, 0.366003, 0.364605, 0.362995, 0.361223,
0.359317, 0.357291, 0.355163, 0.35295, 0.350665, 0.34832, 0.345926,
0.343492, 0.341029, 0.338544, 0.336047, 0.333531, 0.330992, 0.32844,
0.325888, 0.323344, 0.320813, 0.318297, 0.315795, 0.313317, 0.310862,
0.308435, 0.306042, 0.303687, 0.301371, 0.299096, 0.296864, 0.294674,
0.292529, 0.290428, 0.288373, 0.286364, 0.284403, 0.282491, 0.28063,
0.27882, 0.277068, 0.275369, 0.273749, 0.272168, 0.270772, 0.26923,
0.273745, 0.355185, 0.363629, 0.367413, 0.365998, 0.364098, 0.362012,
0.359662, 0.357117, 0.354424, 0.351611, 0.348708, 0.345741, 0.34273,
0.33969, 0.336636, 0.333585, 0.330548, 0.327536, 0.324558, 0.321613,
0.318697, 0.315811, 0.312956, 0.310134, 0.307352, 0.30461, 0.301911,
0.299262, 0.296662, 0.294113, 0.291616, 0.289172, 0.28678, 0.28444,
0.282152, 0.279915, 0.277729, 0.275594, 0.27351, 0.271478, 0.269499,
0.267573, 0.265703, 0.263888, 0.262135, 0.260439, 0.258827, 0.257257,
0.255879, 0.254365, 0.257847, 0.355185, 0.362998, 0.365966, 0.363626,
0.360696, 0.357498, 0.353976, 0.350253, 0.346405, 0.342487, 0.338546,
0.334619, 0.330732, 0.326901, 0.323142, 0.319466, 0.315882, 0.312397,
0.309015, 0.305731, 0.302538, 0.299427, 0.296387, 0.293415, 0.290512,
0.287676, 0.284912, 0.28222, 0.279597, 0.277043, 0.274554, 0.272128,
0.269761, 0.267452, 0.2652, 0.263004, 0.260862, 0.258774, 0.25674,
0.25476, 0.252835, 0.250965, 0.249152, 0.247395, 0.245701, 0.244066,
0.242517, 0.24101, 0.239702, 0.238267, 0.241565, 0.355185, 0.361546,
0.363057, 0.359114, 0.354442, 0.349417, 0.344091, 0.33868, 0.333301,
0.328027, 0.322914, 0.317991, 0.313262, 0.30873, 0.304388, 0.300235,
0.296262, 0.292463, 0.288831, 0.285358, 0.282031, 0.278838, 0.275759,
0.272781, 0.269892, 0.267084, 0.264359, 0.261717, 0.259156, 0.256676,
0.254271, 0.251936, 0.249667, 0.247459, 0.245309, 0.243213, 0.241171,
0.239185, 0.237251, 0.235371, 0.233545, 0.231772, 0.230054, 0.228393,
0.226794, 0.225254, 0.2238, 0.222388, 0.221181, 0.219853, 0.223142,
0.355185, 0.358426, 0.357147, 0.350153, 0.342432, 0.334571, 0.326802,
0.319386, 0.312407, 0.305876, 0.299775, 0.294077, 0.288752, 0.283769,
0.279097, 0.27471, 0.270587, 0.266702, 0.26303, 0.259559, 0.256273,
0.253155, 0.250185, 0.247341, 0.244601, 0.24195, 0.239378, 0.236884,
0.234466, 0.232124, 0.229851, 0.227647, 0.225509, 0.223431, 0.221411,
0.219449, 0.217544, 0.215694, 0.213897, 0.21215, 0.210456, 0.208813,
0.207225, 0.205691, 0.204217, 0.2028, 0.201471, 0.200178, 0.199102,
0.197898, 0.201424, 0.355185, 0.351162, 0.344025, 0.331348, 0.318952,
0.307578, 0.297319, 0.288221, 0.280144, 0.27292, 0.266391, 0.260433,
0.254969, 0.249936, 0.245284, 0.240977, 0.236985, 0.233269, 0.22979,
0.226527, 0.223459, 0.22057, 0.217835, 0.215235, 0.212749, 0.210354,
0.208034, 0.205778, 0.203583, 0.201449, 0.199372, 0.197355, 0.195397,
0.193494, 0.191649, 0.189862, 0.188129, 0.18645, 0.18482, 0.183236,
0.181703, 0.180218, 0.178785, 0.177401, 0.176074, 0.174801, 0.173616,
0.172457, 0.171535, 0.170464, 0.174493, 0.355185, 0.332171, 0.312964,
0.291611, 0.27427, 0.260516, 0.249259, 0.239881, 0.231899, 0.224974,
0.218858, 0.213366, 0.208383, 0.203828, 0.199648, 0.195798, 0.192242,
0.188944, 0.185872, 0.183002, 0.180315, 0.177793, 0.175412, 0.173158,
0.171012, 0.168948, 0.166953, 0.165012, 0.163117, 0.161275, 0.159479,
0.157734, 0.156041, 0.154393, 0.152796, 0.151249, 0.149745, 0.148289,
0.146876, 0.145504, 0.14418, 0.142895, 0.14166, 0.140468, 0.139328,
0.138236, 0.13723, 0.136238, 0.135498, 0.134582, 0.139459, 0.355185,
0.282913, 0.24435, 0.215714, 0.197642, 0.185308, 0.175938, 0.16843,
0.162168, 0.156806, 0.152122, 0.147953, 0.14419, 0.140766, 0.137634,
0.134754, 0.132089, 0.129614, 0.127312, 0.125167, 0.123165, 0.121292,
0.119524, 0.117856, 0.116273, 0.114751, 0.113282, 0.111856, 0.110461,
0.109108, 0.107786, 0.106505, 0.105263, 0.104052, 0.102877, 0.101737,
0.100625, 0.099551, 0.098509, 0.097498, 0.096526, 0.095582, 0.094679,
0.093808, 0.092977, 0.092184, 0.091463, 0.09074, 0.090249, 0.089581,
0.094982, 0.355185, 0.179687, 0.128743, 0.10633, 0.095915, 0.089352,
0.084455, 0.080592, 0.077389, 0.074657, 0.07228, 0.070174, 0.068279,
0.066559, 0.06499, 0.06355, 0.062219, 0.060983, 0.059834, 0.058765,
0.05777, 0.056844, 0.055967, 0.055143, 0.054365, 0.053615, 0.052894,
0.052197, 0.051511, 0.050851, 0.050202, 0.049576, 0.04897, 0.048376,
0.047801, 0.047243, 0.046694, 0.04617, 0.045659, 0.045162, 0.044689,
0.044226, 0.043788, 0.043365, 0.042961, 0.042579, 0.042237, 0.041884,
0.041676, 0.041351, 0.046253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = 3000:3621, class = "data.frame")
CodePudding user response:
You don't have to use for loop
, you can use
J$distance <- c(0 , abs(diff(J$X.wcentroid..cm.)))
J$fishspeed <- J$distance <- c(0 , diff(J$X.wcentroid..cm.)) / 0.02
then you can try this to add velocity
for(i in 1:nrow(J)){
y_min <- CFD_L2$ypos[which.min(abs(J$Y.wcentroid..cm.[i] - CFD_L2$ypos))]
x_min <- which.min(abs(J$X.wcentroid..cm.[i] - subset(CFD_L2 , ypos == y_min)$ï..xpos))
J$velocity[i] <- subset(CFD_L2 , ypos == y_min)$velocity[x_min]
}