R 查找与函数值/总计的输出相对应的行
我的数据与以下类似:R 查找与函数值/总计的输出相对应的行,r,R,我的数据与以下类似: # A tibble: 7 x 3 # Groups: offense [7] lon lat offense <dbl> <dbl> <fct> 1 -95.3 29.8 aggravated assault 2 -95.4 29.9 auto theft 3 -95.3 29.8 burglary 4 -95.5 29.7
# A tibble: 7 x 3
# Groups: offense [7]
lon lat offense
<dbl> <dbl> <fct>
1 -95.3 29.8 aggravated assault
2 -95.4 29.9 auto theft
3 -95.3 29.8 burglary
4 -95.5 29.7 murder
5 -95.4 30.0 rape
6 -95.5 29.8 robbery
7 -95.4 29.8 theft
一旦我有了这些,我想创建一个列表,这样行1
将是一个包含1
、2
、4
和7
列表的列表。(但这可能是另一个问题)
数据:
库(geosphere)
图书馆(ggmap)
df%
组别(进攻)%>%
样本n(1)%>%
选择(长、宽、进攻)
是来自地球圈的犯罪
数据
对不起,它来自库(ggmap)
值不可复制,因为我得到了不同的值Rhapsm1您可以为列表
列分配df$点
cbind(df, X = rowSums(distm(df[,1:2], fun = distHaversine) / 1000 <= 10))
# A tibble: 7 x 4
# Groups: offense [7]
lon lat offense X
<dbl> <dbl> <fct> <dbl>
1 -95.3 29.8 aggravated assault 3
2 -95.4 29.9 auto theft 2
3 -95.3 29.8 burglary 3
4 -95.5 29.7 murder 1
5 -95.4 30.0 rape 2
6 -95.5 29.8 robbery 1
7 -95.4 29.8 theft 3
lon lat offense X points
1 -95.3 29.8 aggravated assault 3 c(2,4,7)
2 -95.4 29.9 auto theft 2 c(2,3)
3 -95.3 29.8 burglary 3 c(4,5,7)
library(geosphere)
library(ggmap)
df <- crime %>%
group_by(offense) %>%
sample_n(1) %>%
select(lon, lat, offense)