我有一个保存位置和时间的数据框。
df <- data.frame(time = c("2017-07-26 07:00:01", "2017-07-26 07:00:01", "2017-07-26 07:00:01", "2017-07-26 07:00:01", "2017-07-26 07:00:02", "2017-07-26 07:00:02"),
east = c(640348.4, 640348.8, 640348.9, 640348.7, 640348.7, 640348.8),
north = c(4858732.0, 4858732.0, 4858732.0, 4858732.2, 4858732.2, 4858732.2))
df$time <- as.POSIXct(df$time, tz = 'America/Chicago')
我创建了一个函数来计算每个点与给定参考点之间的距离,并在数据框中添加一列来保存这些计算出的距离。
dist_fun <- function(p1, p2) {sqrt((p2[1]-p1[1])^2 + (p2[2] - p1[2])^2)}
reference_pt <- c(640342.7, 4858714.1)
names(reference_pt) <- c('east', 'north')
df$dist <- dist_fun(df[, c('east', 'north')], reference_pt)
我想要的结果很明显是一个包含四列的数据框,分别命名为time
、east
、north
和dist
:
time east north dist
1 2017-07-26 07:00:01 640348.4 4858732.0 18.78563281
2 2017-07-26 07:00:01 640348.8 4858732.0 18.91084345
3 2017-07-26 07:00:01 640348.9 4858732.0 18.94333656
4 2017-07-26 07:00:01 640348.7 4858732.2 19.06856051
5 2017-07-26 07:00:02 640348.7 4858732.2 19.06856051
6 2017-07-26 07:00:02 640348.8 4858732.2 19.10026178
但是,我得到的输出是
time east north east
1 2017-07-26 07:00:01 640348.4 4858732.0 18.78563281
2 2017-07-26 07:00:01 640348.8 4858732.0 18.91084345
3 2017-07-26 07:00:01 640348.9 4858732.0 18.94333656
4 2017-07-26 07:00:01 640348.7 4858732.2 19.06856051
5 2017-07-26 07:00:02 640348.7 4858732.2 19.06856051
6 2017-07-26 07:00:02 640348.8 4858732.2 19.10026178
由于某种原因,最后一列被命名为east
,即使我将其定义为df$dist
!修复它不仅仅是一个简单的名称更改,因为当我检查数据框的名称时..
names(df)
[1] "time" "east" "north" "dist"
那么为什么该列的标题是east
而不是dist
?