我有一个保存位置和时间的数据框。
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
?
答案 0 :(得分:1)
您的问题是您的函数返回一个data.frame,并且data.frame具有与您的操作相关联的列名。
我建议您更像这样:
require(dplyr)
df %>% mutate(dist = sqrt((reference_pt[1] - east)^2 + (reference_pt[2] - north)^2))
time east north dist
1 2017-07-26 07:00:01 640348.4 4858732 18.78563
2 2017-07-26 07:00:01 640348.8 4858732 18.91084
3 2017-07-26 07:00:01 640348.9 4858732 18.94334
4 2017-07-26 07:00:01 640348.7 4858732 19.06856
5 2017-07-26 07:00:02 640348.7 4858732 19.06856
6 2017-07-26 07:00:02 640348.8 4858732 19.10026
如果您愿意使用此功能,它将按以下方式工作:
dist_fun <- function(p1, p2) {
distCalc <- sqrt((p2[1]-p1[1])^2 + (p2[2] - p1[2])^2)
names(distCalc) <- "dist"
return(distCalc)
}
df <- cbind(df, dist_fun(df[, c('east', 'north')], reference_pt))
还请注意,您的原始操作会在data.frame中创建一个data.frame:
str(df)
'data.frame': 6 obs. of 4 variables:
$ time : POSIXct, format: "2017-07-26 07:00:01" "2017-07-26 07:00:01" "2017-07-26 07:00:01" "2017-07-26 07:00:01" ...
$ east : num 640348 640349 640349 640349 640349 ...
$ north: num 4858732 4858732 4858732 4858732 4858732 ...
$ dist :'data.frame': 6 obs. of 1 variable:
..$ east: num 18.8 18.9 18.9 19.1 19.1 ...
如果您不知道自己有嵌套的对象,这会导致其他操作出现问题。