我有一个保存位置和时间的数据框架。
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呢
发布于 2018-08-04 03:13:01
您的问题是,您的函数返回一个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 ...如果你没有意识到你有一个嵌套的对象,这可能会导致其他操作的问题。
https://stackoverflow.com/questions/51678661
复制相似问题