过滤R中的两个变量

时间:2017-08-07 20:04:01

标签: r

我的数据集有57个位置"家庭"对于每个位置,我有10个其他位置"气象站"以英里为单位的近距离排名。然后我有一个柱子,有气象站落在房子周围的象限,所以1-4。 我正在尝试在R中编写代码,该代码采用最近的气象站及其象限,然后在不同的象限中采用下一个最接近的象限,在不同的象限中采用第三个。所以我有一个基于最近的车站的三角形。

    loc           station                   nearness  quadrant 
1   Abilene-KS   SALINA MUNICIPAL AIRPORT       1         2
2   Abilene-KS   MARSHALL ARMY AIRFIELD         2         1
3   Abilene-KS   MULTI PURPOSE RANGE            3         1
4   Abilene-KS   MANHATTAN REGIONAL AIRPORT     4         3
5   Abilene-KS   MANHATTAN 6 SSW                5         1 
6   Abilene-KS   BLOSSER MUNICIPAL AIRPORT      6         4
7   Abilene-KS   NEWTON-CITY-COUNTY AIRPORT     7         1
8   Abilene-KS   EMPORIA MUNICIPAL AIRPORT      8         2 
9   Abilene-KS   HUTCHINSON MUNICIPAL ARPT      9         4
10  Abilene-KS   COLONEL JAMES JABARA ARPT      10        3
11  Archbold-OH  SALINA MUNICIPAL AIRPORT       1         2 
12  Archbold-OH  MARSHALL ARMY AIRFIELD         2         1
13  Archbold-OH  MULTI PURPOSE RANGE            3         3
14  Archbold-OH  MANHATTAN REGIONAL AIRPORT     4         1
15  Archbold-OH  MANHATTAN 6 SSW                5         4   
16  Archbold-OH  BLOSSER MUNICIPAL AIRPORT      6         4
17  Archbold-OH  NEWTON-CITY-COUNTY AIRPORT     7         2
18  Archbold-OH  EMPORIA MUNICIPAL AIRPORT      8         1
19  Archbold-OH  HUTCHINSON MUNICIPAL ARPT      9         3
20  Archbold-OH  COLONEL JAMES JABARA ARPT      10        2

我希望我可以说我有一些代码来展示我所尝试的但是一切都让我无处可去,所以我迷失了方向。有任何想法吗?

对于这两个位置,我想要一个带

的新数据框
    loc           station                   nearness  quadrant 
1   Abilene-KS   SALINA MUNICIPAL AIRPORT       1         2
2   Abilene-KS   MARSHALL ARMY AIRFIELD         2         1
3   Abilene-KS   MANHATTAN REGIONAL AIRPORT     4         3
4   Archbold-OH  SALINA MUNICIPAL AIRPORT       1         2 
5   Archbold-OH  MARSHALL ARMY AIRFIELD         2         1
6   Archbold-OH  MULTI PURPOSE RANGE            3         3

1 个答案:

答案 0 :(得分:1)

使用dplyr,您可以:

library(dplyr)
df %>% 
distinct(loc,quadrant,.keep_all=T) %>%
group_by(loc) %>%
top_n(-3,nearness)

返回:

          loc                    station nearness quadrant
        <chr>                      <chr>    <int>    <int>
1  Abilene-KS   SALINA MUNICIPAL AIRPORT        1        2
2  Abilene-KS     MARSHALL ARMY AIRFIELD        2        1
3  Abilene-KS MANHATTAN REGIONAL AIRPORT        4        3
4 Archbold-OH   SALINA MUNICIPAL AIRPORT        1        2
5 Archbold-OH     MARSHALL ARMY AIRFIELD        2        1
6 Archbold-OH        MULTI PURPOSE RANGE        3        3