r语言 - 如何根据if_else语句改变总结X行数的新变量?



我有一个空间数据帧,其中包含每秒的GPS点以及点与点之间的距离和转弯角度。我想创建一个新的专栏,总结行驶10米所需的时间和过去10米的平均转弯角度。

<表类> 行xy日期dxdydistdtR2nabs.anglerel.angletbody><<tr>1580988。12981141。2021-07-18 07:00:07-1.99-0.01281.9910-3.14NA2580986。12981141。2021-07-18 07:00:0800013.94NANA3580986。12981141。2021-07-18 07:00:092.971.133.1813.940.363-2.794580989。12981142。2021-07-18 07:00:1000012.21NANA

有一种(有点笨拙的)方法可以做到这一点。我不确定你想如何按距离分组你的数据。在这里,我假设第一行为0,并将数据分组为0-9、10-19、20-29等。这是你所期望的吗?

library(dplyr)
# Calculate distance between consecutive rows
df$dist <- c(0, sqrt(diff(df$x)^2 + diff(df$y)^2))
# Calculate a rolling distance
df$roll <- cumsum(df$dist)
# Group rows by every 10 meters
df$grp <- factor(floor(df$roll/10) * 10)
# Calculate summary values by group
summ <- df %>%
group_by(grp) %>%
summarize(
sum.dt = sum(dt),
average.rel.angle = mean(rel.angle, na.rm = TRUE))
# Combine summary with original data
merge(df, summ, by="grp")

数据:

structure(list(row = c("1", "2", "3", "4", "5", "6", "7"), x = c(580985, 
580990, 580995, 581000, 581005, 581010, 581015), y = c(12981141, 
12981141, 12981141, 12981141, 12981141, 12981141, 12981141), 
date = structure(list(sec = c(7, 8, 9, 10, 11, 12, 13), min = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L), hour = c(7L, 7L, 7L, 7L, 7L, 7L, 
7L), mday = c(18L, 18L, 18L, 18L, 18L, 18L, 18L), mon = c(6L, 
6L, 6L, 6L, 6L, 6L, 6L), year = c(121L, 121L, 121L, 121L, 
121L, 121L, 121L), wday = c(0L, 0L, 0L, 0L, 0L, 0L, 0L), 
yday = c(198L, 198L, 198L, 198L, 198L, 198L, 198L), isdst = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L), zone = c("EDT", "EDT", "EDT", 
"EDT", "EDT", "EDT", "EDT"), gmtoff = c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_)), class = c("POSIXlt", "POSIXt")), dx = c(-1.99, 
0, 2.97, 0, 0, 0, 0), dy = c(-0.0128, 0, 1.13, 0, 0, 0, 0
), dist = c(1.99, 0, 3.18, 0, 0, 0, 0), dt = c(1, 1, 1, 1, 
1, 1, 1), R2n = c(0, 3.94, 3.94, 2.21, 2.21, 2.21, 2.21), 
abs.angle = c(-3.14, NA, 0.363, NA, NA, NA, NA), rel.angle = c(NA, 
NA, -2.79, NA, NA, NA, NA)), row.names = c("a", "b", "d", 
"e", "f", "g", "h"), class = "data.frame")

相关内容

  • 没有找到相关文章

最新更新