R比较纬度并汇总每行的结果
R Compare latitudes and summarise results for each row
我有一个租赁广告数据集。对于每个广告,我都有其精确的纬度(和经度,但为了简单起见,我们现在只使用纬度)。对于每个纬度,我都计算了大约边界。向北和向南 2 公里(latmin 和 latmax):
advert_id locality_public_lat latmin latmax
1 10083631 49.5794 49.56143 49.59737
2 10247827 50.0546 50.03663 50.07257
3 10647798 50.0910 50.07303 50.10897
4 10584515 50.0906 50.07263 50.10857
我现在想知道每个广告有多少其他广告落在这些边界内(获取 locality_public_lat 列中的所有值并将其与第一行中的 latmin 和 latmax 进行比较并得出一个计数第一行;然后对所有其他行重复)。
到目前为止,我尝试使用汇总函数聚合数据,但问题是它只比较每行内的纬度,因此结果无效(只计算所有行)。
summary <- data_prodej_3 %>%
group_by(advert_id, month_stats) %>%
summarise(pocet_inz = sum(locality_public_lat > latmin & locality_public_lat < latmax))
有没有办法按照我描述的方式总结数据?
猜测您需要使用一个函数和一些循环来工作。
您的数据保存在名为 lat.txt:
的文件中
dvert_id,locality_public_lat,latmin,latmax
10083631,49.5794,49.56143,49.59737
10247827,50.0546,50.03663,50.07257
10647798,50.0910,50.07303,50.10897
10584515,50.0906,50.07263,50.10857
我们可以使用:
library (tidyverse)
#load the data
data_prodej_3 <- read.csv(file='lat.txt')
#save the dvert_id list
dvert_id_vector <- data_prodej_3[,c(1)]
#initialize the final recap table
recap_table <- data.frame(cbind(dvert_id=character(),locality_public_lat=numeric(),occurrences=integer()))
for (i in dvert_id_vector) {
counter <- 0
#save the locality_public_lat we're interested to compare
locality_public_lat_value <- data_prodej_3[data_prodej_3$dvert_id==i,c(2)]
#remove the dvert_id we're examining
data_prodej_3_filtered <- data_prodej_3[data_prodej_3$dvert_id!=i,]
for (j in seq(1,length(data_prodej_3_filtered)-1)) {
if (locality_public_lat_value > data_prodej_3_filtered[j,c(3)] & locality_public_lat_value < data_prodej_3_filtered[j,c(4)]) {
counter <- counter + 1
}
}
#save an entry in the recap table with the resulting count for the particular dvert_id
recap_table <- rbind(recap_table, c(i,locality_public_lat_value,counter))
}
colnames(recap_table) <- c('dvert_id','locality_public_lat','occurrences')
recap_table
获得:
dvert_id locality_public_lat occurrences
1 10083631 49.5794 0
2 10247827 50.0546 0
3 10647798 50.0910 1
4 10584515 50.0906 1
我有一个租赁广告数据集。对于每个广告,我都有其精确的纬度(和经度,但为了简单起见,我们现在只使用纬度)。对于每个纬度,我都计算了大约边界。向北和向南 2 公里(latmin 和 latmax):
advert_id locality_public_lat latmin latmax
1 10083631 49.5794 49.56143 49.59737
2 10247827 50.0546 50.03663 50.07257
3 10647798 50.0910 50.07303 50.10897
4 10584515 50.0906 50.07263 50.10857
我现在想知道每个广告有多少其他广告落在这些边界内(获取 locality_public_lat 列中的所有值并将其与第一行中的 latmin 和 latmax 进行比较并得出一个计数第一行;然后对所有其他行重复)。
到目前为止,我尝试使用汇总函数聚合数据,但问题是它只比较每行内的纬度,因此结果无效(只计算所有行)。
summary <- data_prodej_3 %>%
group_by(advert_id, month_stats) %>%
summarise(pocet_inz = sum(locality_public_lat > latmin & locality_public_lat < latmax))
有没有办法按照我描述的方式总结数据?
猜测您需要使用一个函数和一些循环来工作。
您的数据保存在名为 lat.txt:
的文件中dvert_id,locality_public_lat,latmin,latmax
10083631,49.5794,49.56143,49.59737
10247827,50.0546,50.03663,50.07257
10647798,50.0910,50.07303,50.10897
10584515,50.0906,50.07263,50.10857
我们可以使用:
library (tidyverse)
#load the data
data_prodej_3 <- read.csv(file='lat.txt')
#save the dvert_id list
dvert_id_vector <- data_prodej_3[,c(1)]
#initialize the final recap table
recap_table <- data.frame(cbind(dvert_id=character(),locality_public_lat=numeric(),occurrences=integer()))
for (i in dvert_id_vector) {
counter <- 0
#save the locality_public_lat we're interested to compare
locality_public_lat_value <- data_prodej_3[data_prodej_3$dvert_id==i,c(2)]
#remove the dvert_id we're examining
data_prodej_3_filtered <- data_prodej_3[data_prodej_3$dvert_id!=i,]
for (j in seq(1,length(data_prodej_3_filtered)-1)) {
if (locality_public_lat_value > data_prodej_3_filtered[j,c(3)] & locality_public_lat_value < data_prodej_3_filtered[j,c(4)]) {
counter <- counter + 1
}
}
#save an entry in the recap table with the resulting count for the particular dvert_id
recap_table <- rbind(recap_table, c(i,locality_public_lat_value,counter))
}
colnames(recap_table) <- c('dvert_id','locality_public_lat','occurrences')
recap_table
获得:
dvert_id locality_public_lat occurrences
1 10083631 49.5794 0
2 10247827 50.0546 0
3 10647798 50.0910 1
4 10584515 50.0906 1