R 基于其他列添加具有函数结果的列
我有以下数据框:R 基于其他列添加具有函数结果的列,r,dataframe,calculated-columns,R,Dataframe,Calculated Columns,我有以下数据框: Latitude , Longitude, Altitude 44.388401, 8.433392 , 463.000000 44.388571, 8.434575 , 471.000000 44.388740, 8.435758 , 507.000000 44.388910, 8.436941 , 563.000000 44.389079, 8.438123 , 606.000000 44.389249, 8.439306 , 629.000000 44.389418, 8
Latitude , Longitude, Altitude
44.388401, 8.433392 , 463.000000
44.388571, 8.434575 , 471.000000
44.388740, 8.435758 , 507.000000
44.388910, 8.436941 , 563.000000
44.389079, 8.438123 , 606.000000
44.389249, 8.439306 , 629.000000
44.389418, 8.440489 , 639.000000
44.389588, 8.441672 , 640.000000
44.389757, 8.442854 , 590.000000
44.389927, 8.444037 , 564.000000
44.390096, 8.445220 , 543.000000
44.390265, 8.446403 , 527.000000
44.390435, 8.447585 , 469.000000
前两列是纬度和经度(以度为单位),第三列是海拔高度。我想做的是添加一列,表示观察位置与第一次观察位置之间的距离,类似于(距离不精确,只是为了显示)
我知道我可以使用librarygeosphere
中的函数distm
,但问题是:如何添加一个列,该列的值是由一个函数计算的,该函数的参数包括相同观测值的其他值和第一个观测值
我已经看到了,但它允许根据相同观察值的其他数据计算新列,而不是相同的观察值和第一个数据,就像我需要的那样。不确定为什么
distm
函数是这样编写的,但这应该可以工作:
library(dplyr)
# Put the data in a data frame
df <- data.frame(Latitude = c(44.388401,44.388571), Longitude = c(8.433392,8.434575), Altitude = c(471.000000, 463.000000))
# Extract the two required columns
start_point <- df %>% select(Longitude, Latitude) %>% filter(row_number() == 1)
lat_long <- select(df, Longitude, Latitude)
# Calculate distance
df %>% mutate(Distance = distm(lat_long, start_point ))
库(dplyr)
#将数据放入数据框中
df%过滤器(行数()==1)
横向长度%变异(距离=距离(横向长度,起点))
如果我正确理解了问题,那么您可以使用pmap\u dbl
frompurrr
库(dplyr)
图书馆(地球圈)
图书馆(purrr)
df%>%
变异(距离=pmap_dbl(,~distm(c(…2,…1)),
c(经度[1],纬度[1]),
乐趣=消遣)
样本数据:
df <- structure(list(Latitude = c(44.388401, 44.388571, 44.38874, 44.38891,
44.389079, 44.389249, 44.389418, 44.389588, 44.389757, 44.389927,
44.390096, 44.390265, 44.390435), Longitude = c(8.433392, 8.434575,
8.435758, 8.436941, 8.438123, 8.439306, 8.440489, 8.441672, 8.442854,
8.444037, 8.44522, 8.446403, 8.447585), Altitude = c(463, 471,
507, 563, 606, 629, 639, 640, 590, 564, 543, 527, 469)), .Names = c("Latitude",
"Longitude", "Altitude"), class = "data.frame", row.names = c(NA,
-13L))
df
df <- structure(list(Latitude = c(44.388401, 44.388571, 44.38874, 44.38891,
44.389079, 44.389249, 44.389418, 44.389588, 44.389757, 44.389927,
44.390096, 44.390265, 44.390435), Longitude = c(8.433392, 8.434575,
8.435758, 8.436941, 8.438123, 8.439306, 8.440489, 8.441672, 8.442854,
8.444037, 8.44522, 8.446403, 8.447585), Altitude = c(463, 471,
507, 563, 606, 629, 639, 640, 590, 564, 543, 527, 469)), .Names = c("Latitude",
"Longitude", "Altitude"), class = "data.frame", row.names = c(NA,
-13L))