我正在努力设计一个高效的解决方案,该解决方案运行在14*10e6 records
之上,并且能够根据其以前的element_id
为每个element_id
分配difference
(-
)。显然,对于每个element_id == 1
,增量总是等于NA
,因为它没有以前的元素可以比较。
考虑到一个data.frame,如下所示:
set.seed(1234)
ID <- c(rep(1, 6), rep(2, 5))
element_id <- c(seq.int(1, 6), seq.int(1, 5))
degree <- as.integer(runif(11, 0, 360)) #angular degrees goes from 0 to 359 because 0 is also 360.
mydf <- data.frame(ID, element_id, degree)
与其他与连续行之间的差异有关的问题不同的是,如果element_id
i
等于350
,而element_id
i+1
等于10
,则差异应该是20
。
发布于 2017-12-15 01:39:06
您可以尝试函数getDifference()
。函数getDifference()
180
添加到该差异中360
(%% 360
)的模并减去180
代码:
# Function to calculate difference in degrees
getDifference <- function(degreeA = 0, degreeB = 0) {
(degreeA - degreeB + 180) %% 360 - 180
}
# Test function
getDifference(10, 350)
# [1] 20
getDifference(350, 10)
# [1] -20
适用于操作规程数据
# 1. Get difference with previous row (data.table shift)
# 2. For each ID is done using data.table by
library(data.table)
setDT(mydf)
mydf[, degreeDiff := getDifference(degree, shift(degree)), ID]
# ID element_id degree degreeDiff
# 1: 1 1 40 NA
# 2: 1 2 224 -176
# 3: 1 3 219 -5
# 4: 1 4 224 5
# 5: 1 5 309 85
# 6: 1 6 230 -79
# 7: 2 1 3 NA
# 8: 2 2 83 80
# 9: 2 3 239 156
#10: 2 4 185 -54
#11: 2 5 249 64
https://stackoverflow.com/questions/47828748
复制相似问题