tidyverse error Error: All select() inputs must resolve to integer column positions
tidyverse error Error: All select() inputs must resolve to integer column positions
由于这个错误,我在执行我的脚本时遇到了问题:
Error: All select() inputs must resolve to integer column positions.
我从这些链接中发现了同样的错误:
我正在使用 R 计算经纬度点之间的距离。
这是我的脚本:
library(tidyverse)
#functions
dms_to_rad <- function(d, m, s) (d + m / 60 + s / 3600) * pi / 180
great_circle_distance <- function(lat1, long1, lat2, long2) {
a <- sin(0.5 * (lat2 - lat1))
b <- sin(0.5 * (long2 - long1))
12742 * asin(sqrt(a * a + cos(lat1) * cos(lat2) * b * b))
}
#read file
dir1 = "JTWC_1979.csv"
dir2 = "Ambulong_proc_1979.csv"
jtwc <- read.csv(dir1) %>%
unite('key',c('Year','Month','Day','Hour'))
stn <- read.csv(dir2) %>%
unite('key',c('Year','Month','Day','Hour'))
#aggregating
stn <- left_join(jtwc,stn,by = "key") %>%
drop_na() %>%
mutate_at(vars(Lat.x,Lon.x,Lat.y,Lon.y),funs(dms_to_rad(.,0,0))) %>%
mutate(dist = great_circle_distance(Lat.x,Lon.x,Lat.y,Lon.y))
write.csv(stn,file="dist.csv",row.names=T)
文件 1:
SN CY Year Month Day Hour Lat Lon
196101 1 1961 1 14 12 8.3 134.7
196101 1 1961 1 14 18 8.8 133.4
196101 1 1961 1 15 0 9.1 132.5
196101 1 1961 1 15 6 9.3 132.2
196101 1 1961 1 15 12 9.5 132
196101 1 1961 1 15 18 9.9 131.8
196125 1 1961 1 14 12 10.0 136
196125 1 1961 1 14 18 10.5 136.5
文件 2:
Year Month Day RR Hour Lat Lon
1961 1 14 0 0 14.0917 121.055
1961 1 14 0 6 14.0917 121.055
1961 1 14 0 12 14.0917 121.055
1961 1 14 0 18 14.0917 121.055
1961 1 15 0 0 14.0917 121.055
1961 1 15 0 6 14.0917 121.055
"SN" 列是 file1 中的唯一标识符。我想做什么:
[1]计算两个文件的年月日时相同时的距离(jtwc$dist)
[2]如果file1中有相同年月日小时但SN号不同的行,我将使用相同年月日小时的行中的值file2在计算距离。
关于如何正确执行此操作的任何建议?
您应该已经阅读了有关 ?mutate_at
的内容,在那里您可以找到很多如何使用它的示例;你的问题是你将实际列传递给 .vars 而你应该使用以下之一:
- dplyr select 辅助函数之一参见
?select_helpers
和 vars
;
- 列名作为字符向量;
- 作为数字向量的列位置;
vars
带有序列 colA:colB
符号;
对于您的情况,您只需要一个列名向量,此外不需要在 mutate
中使用 df$...
引用父数据框,只需使用列名:
left_join(jtwc,stn,by = "key") %>%
drop_na() %>%
mutate_at(c("Lat.x", "Lon.x", "Lat.y", "Lon.y"), funs(dms_to_rad(., 0, 0))) %>%
mutate(dist = great_circle_distance(Lat.x, Lon.x, Lat.y, Lon.y))
# SN CY key Lat.x Lon.x RR Lat.y Lon.y dist
#1 196101 1 1961_1_14_12 0.1448623 2.350959 0 0.2459466 2.112808 1620.961
#2 196101 1 1961_1_14_18 0.1535890 2.328269 0 0.2459466 2.112808 1467.859
#3 196101 1 1961_1_15_0 0.1588250 2.312561 0 0.2459466 2.112808 1364.150
#4 196101 1 1961_1_15_6 0.1623156 2.307325 0 0.2459466 2.112808 1324.915
#5 196125 1 1961_1_14_12 0.1745329 2.373648 0 0.2459466 2.112808 1687.127
#6 196125 1 1961_1_14_18 0.1832596 2.382374 0 0.2459466 2.112808 1724.351
由于这个错误,我在执行我的脚本时遇到了问题:
Error: All select() inputs must resolve to integer column positions.
我从这些链接中发现了同样的错误:
我正在使用 R 计算经纬度点之间的距离。 这是我的脚本:
library(tidyverse)
#functions
dms_to_rad <- function(d, m, s) (d + m / 60 + s / 3600) * pi / 180
great_circle_distance <- function(lat1, long1, lat2, long2) {
a <- sin(0.5 * (lat2 - lat1))
b <- sin(0.5 * (long2 - long1))
12742 * asin(sqrt(a * a + cos(lat1) * cos(lat2) * b * b))
}
#read file
dir1 = "JTWC_1979.csv"
dir2 = "Ambulong_proc_1979.csv"
jtwc <- read.csv(dir1) %>%
unite('key',c('Year','Month','Day','Hour'))
stn <- read.csv(dir2) %>%
unite('key',c('Year','Month','Day','Hour'))
#aggregating
stn <- left_join(jtwc,stn,by = "key") %>%
drop_na() %>%
mutate_at(vars(Lat.x,Lon.x,Lat.y,Lon.y),funs(dms_to_rad(.,0,0))) %>%
mutate(dist = great_circle_distance(Lat.x,Lon.x,Lat.y,Lon.y))
write.csv(stn,file="dist.csv",row.names=T)
文件 1:
SN CY Year Month Day Hour Lat Lon
196101 1 1961 1 14 12 8.3 134.7
196101 1 1961 1 14 18 8.8 133.4
196101 1 1961 1 15 0 9.1 132.5
196101 1 1961 1 15 6 9.3 132.2
196101 1 1961 1 15 12 9.5 132
196101 1 1961 1 15 18 9.9 131.8
196125 1 1961 1 14 12 10.0 136
196125 1 1961 1 14 18 10.5 136.5
文件 2:
Year Month Day RR Hour Lat Lon
1961 1 14 0 0 14.0917 121.055
1961 1 14 0 6 14.0917 121.055
1961 1 14 0 12 14.0917 121.055
1961 1 14 0 18 14.0917 121.055
1961 1 15 0 0 14.0917 121.055
1961 1 15 0 6 14.0917 121.055
"SN" 列是 file1 中的唯一标识符。我想做什么:
[1]计算两个文件的年月日时相同时的距离(jtwc$dist)
[2]如果file1中有相同年月日小时但SN号不同的行,我将使用相同年月日小时的行中的值file2在计算距离。
关于如何正确执行此操作的任何建议?
您应该已经阅读了有关 ?mutate_at
的内容,在那里您可以找到很多如何使用它的示例;你的问题是你将实际列传递给 .vars 而你应该使用以下之一:
- dplyr select 辅助函数之一参见
?select_helpers
和vars
; - 列名作为字符向量;
- 作为数字向量的列位置;
vars
带有序列colA:colB
符号;
对于您的情况,您只需要一个列名向量,此外不需要在 mutate
中使用 df$...
引用父数据框,只需使用列名:
left_join(jtwc,stn,by = "key") %>%
drop_na() %>%
mutate_at(c("Lat.x", "Lon.x", "Lat.y", "Lon.y"), funs(dms_to_rad(., 0, 0))) %>%
mutate(dist = great_circle_distance(Lat.x, Lon.x, Lat.y, Lon.y))
# SN CY key Lat.x Lon.x RR Lat.y Lon.y dist
#1 196101 1 1961_1_14_12 0.1448623 2.350959 0 0.2459466 2.112808 1620.961
#2 196101 1 1961_1_14_18 0.1535890 2.328269 0 0.2459466 2.112808 1467.859
#3 196101 1 1961_1_15_0 0.1588250 2.312561 0 0.2459466 2.112808 1364.150
#4 196101 1 1961_1_15_6 0.1623156 2.307325 0 0.2459466 2.112808 1324.915
#5 196125 1 1961_1_14_12 0.1745329 2.373648 0 0.2459466 2.112808 1687.127
#6 196125 1 1961_1_14_18 0.1832596 2.382374 0 0.2459466 2.112808 1724.351