【发布时间】:2022-01-10 11:16:24
【问题描述】:
我有一个包含多列的数据框,我正在使用 for 循环来应用记录在新列中的数学运算。数据框名为“F39”。我写的代码如下:
for (i in 2:nrow(F39)) {
#calculating distance from distance formula (both in x and y)
F39$distance[i] <- sqrt((F39$X..cm.[i]-F39$X..cm.[i-1])^2 + (F39$Y..cm.[i]-F39$Y..cm.[i-1])^2)
#calculating fish speed in x and y
F39$fishspeed[i] <- F39$distance[i]/(0.02)
#assigning 0 as the starting fish speed
F39$fishspeed[1] <- 0
#assigning positive and negative signs to the velocity
F39$fishspeed[i] <- ifelse(F39$X..cm.[i]-F39$X..cm.[i-1] < 0,F39$fishspeed[i],-F39$fishspeed[i])
}
但是,它给了我以下错误:
$<-.data.frame(*tmp*, "距离", value = c(NA, 0.194077783375631 中的错误:
替换有2行,数据有4837
我的数据框中有 4837 行。我有许多其他数据帧,我正在应用相同的代码并且它正在工作,但在这里和其他一些数据帧中,它不起作用。
我在 google 驱动器中添加了带有数据的 .CSV 文件:Link to csv file
【问题讨论】: