我的data.frame(movement.data)看起来像这样:
head(movement.data)
Longitude Latitude Sample X Y
1 -1.581638 53.81659 1099.99 95.41699 65.88299
2 -1.581642 53.81659 1100.09 95.17410 65.89411
3 -1.581646 53.81659 1100.19 94.93121 65.80516
4 -1.581650 53.81659 1100.30 94.65550 65.74956
5 -1.581650 53.81659 1100.30 94.65550 65.74956
6 -1.581654 53.81659 1100.49 94.41261 65.60501
7 -1.581657 53.81659 1100.59 94.20254 65.59389
8 -1.581660 53.81659 1100.69 93.97934 65.57165
9 -1.581662 53.81659 1100.80 93.88088 65.54941
10 -1.581662 53.81659 1100.80 93.88088 65.54941
11 -1.581661 53.81659 1100.90 93.90714 65.42709
12 -1.581661 53.81659 1101.00 93.89401 65.50493
13 -1.581660 53.81659 1101.19 93.96621 65.69396
14 -1.581658 53.81659 1101.29 94.12376 65.76068
> structure(list(Longitude = c(-1.5816382, -1.5816419, -1.5816456,
-1.5816498, -1.5816498, -1.5816535, -1.5816567, -1.5816601, -1.5816616,
-1.5816616, -1.5816612, -1.5816614, -1.5816603, -1.5816579, -1.5816551,
-1.581652), Latitude = c(53.8165922, 53.8165923, 53.8165915,
53.816591, 53.816591, 53.8165897, 53.8165896, 53.8165894, 53.8165892,
53.8165892, 53.8165881, 53.8165888, 53.8165905, 53.8165911, 53.8165925,
53.8165926), Sample = c(1099.99, 1100.09, 1100.19, 1100.3, 1100.3,
1100.49, 1100.59, 1100.69, 1100.8, 1100.8, 1100.9, 1101, 1101.19,
1101.29, 1101.39, 1101.5), X = c(95.4169935430477, 95.1741017603222,
94.9312120174133, 94.6554984032894, 94.6554984032894, 94.4126097693692,
94.2025416091894, 93.9793443981241, 93.8808752899916, 93.8808752899916,
93.9071363032427, 93.8940054603786, 93.9662126575805, 94.1237625957496,
94.3075692760132, 94.5110727848407), Y = c(65.8829940372709,
65.8941135300653, 65.8051575885002, 65.7495601253183, 65.7495601253183,
65.6050067205713, 65.5938872277769, 65.5716482421881, 65.5494092573894,
65.5494092573894, 65.4270948374411, 65.5049312862118, 65.6939626613463,
65.7606796173226, 65.9163525148641, 65.9274720076584)), row.names = 11000:11015, class = "data.frame")
我想计算X和Y坐标之间的角位移。我编写了一个for循环来遍历data.frame(请参见下面的代码)。
Angular.Displacement <- vector("numeric", length(movement.data$X))
for (dp in movement.data) {
i <- 2
while(i <= length(movement.data$X)) {
a <- c((movement.data$X[i] - movement.data$X[i - 1]), (movement.data$Y[i] - movement.data$Y[i - 1]))
b <- c((movement.data$X[i + 1] - movement.data$X[i]), (movement.data$Y[i + 1] - movement.data$Y[i]))
dot.prod <- a %*% b
norm.a <- norm(a, type = "2")
norm.b <- norm(b, type = "2")
Angular.Displacement[i] = acos((dot.prod) / (norm.a * norm.b)) * (180 / pi)
i <- i+1
}
}
虽然此代码有效,但它笨拙且效率不高。我将循环转换为一个函数(compute_angular_displacement-参见下文),以便可以使用dplyr库中的mutate将其应用于data.frame。
compute_angular_displacement <- function(df) {
# Calculation coefficients
a <- c((df$X - lag(df$X)), (df$Y - lag(df$Y)))
b <- c((lead(df$X) - df$X), (lead(df$Y) - df$Y))
dot.prod <- a %*% b
norm_a <- norm(a, type = "2")
norm_b <- norm(b, type = "2")
# Calculation for angular displacement
Angular_Displacement = acos((dot.prod)/(norm_a * norm_b)) * (180/pi)
}
但是,当我应用该函数(请参见下面的代码)时,会收到此错误代码(下面是mutate的代码示例):
movement.data <- mutate(movement.data, Angular_Displacement = compute_angular_displacement(movement.data))
svd(x,nu = 0L,nv = 0L)中的错误:'x'中的值无穷或缺失
我在函数外部手动检查了计算结果。当我将方法和计算应用于从shell中的motion.data选择的数据点时,它们返回了正确的答案。
进一步的研究发现,错误是由使用“ lag”和“ lead”函数引起的,当调用它们时,它们会在c()中产生NA值,而当“ norm.a < -运行norm(a,type =“ 2”)“。
有人知道如何解决此问题吗? -一个可能的快速解决方案是我的while循环和函数之间的组合,但是我不确定如何成功实现这一点。在这方面的任何帮助将不胜感激。