我想使我的arrange
通话动态化。当前,所有可能的不同列都可以在下面的数据(SQL_Table)中找到:
# A tibble: 144,000 x 15
plan class gender band marital_status acceleration extension inflation iss_age cell dur db_perk accel_perk ext_perk
<chr> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 1 0.196 0.0035 NA
2 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 2 0.231 0.0148 NA
3 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 3 0.375 0.032 NA
4 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 4 0.399 0.0528 NA
5 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 5 0.451 0.0767 NA
6 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 6 0.513 0.0997 NA
7 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 7 0.595 0.121 NA
8 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 8 0.678 0.145 NA
9 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 9 0.761 0.174 NA
10 Basi~ 111 F 1a Married A.24 E.0 AC3.EC3 40 Basi~ 10 0.844 0.207 NA
# ... with 143,990 more rows, and 1 more variable: attage <dbl>
有时我的数据不会包含所有列。例如,班级和乐队可能会被排除在外:
# A tibble: 144,000 x 13
plan gender marital_status acceleration extension inflation iss_age cell dur db_perk accel_perk ext_perk attage
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 1 0.196 0.0035 NA 41
2 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 2 0.231 0.0148 NA 42
3 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 3 0.375 0.032 NA 43
4 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 4 0.399 0.0528 NA 44
5 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 5 0.451 0.0767 NA 45
6 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 6 0.513 0.0997 NA 46
7 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 7 0.595 0.121 NA 47
8 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 8 0.678 0.145 NA 48
9 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 9 0.761 0.174 NA 49
10 BasicF F Married A.24 E.0 AC3.EC3 40 BasicFF240~ 10 0.844 0.207 NA 50
# ... with 143,990 more rows
我想要一个arrange
调用,该调用将按从左到右以'iss_age'结尾的列排列我的数据,然后'dur'总是在末尾。
对于顶部小标题,我的arrange
呼叫看起来像这样:
SQL_Table %>%
arrange(plan,class, gender,band,marital_status,acceleration, extension, inflation, iss_age, dur)
对于第二个小标题,它看起来像这样:(注意类别和乐队除外)
SQL_Table %>%
arrange(plan,gender,marital_status, acceleration, extension, inflation,iss_age, dur)
可能包含或可能不包含的变量是: 计划,类别,性别,乐队,婚姻状况,加速,扩展和通货膨胀。
将始终包含其余变量。
有没有一种方法可以根据我的数据集中包含哪些列来动态地执行此操作?
答案 0 :(得分:0)
arrange_at
在这里很有用,因为它允许使用vars()
构造来定义列。
一个例子:
as.data.frame(Titanic) %>%
arrange_at(vars(everything()))
Class Sex Age Survived Freq 1 1st Male Child No 0 2 1st Male Child Yes 5 3 1st Male Adult No 118 4 1st Male Adult Yes 57 5 1st Female Child No 0 6 1st Female Child Yes 1 7 1st Female Adult No 4 8 1st Female Adult Yes 140 9 2nd Male Child No 0 10 2nd Male Child Yes 11 11 2nd Male Adult No 154 12 2nd Male Adult Yes 14 13 2nd Female Child No 0 14 2nd Female Child Yes 13 15 2nd Female Adult No 13 16 2nd Female Adult Yes 80 17 3rd Male Child No 35 18 3rd Male Child Yes 13 19 3rd Male Adult No 387 20 3rd Male Adult Yes 75 21 3rd Female Child No 17 22 3rd Female Child Yes 14 23 3rd Female Adult No 89 24 3rd Female Adult Yes 76 25 Crew Male Child No 0 26 Crew Male Child Yes 0 27 Crew Male Adult No 670 28 Crew Male Adult Yes 192 29 Crew Female Child No 0 30 Crew Female Child Yes 0 31 Crew Female Adult No 3 32 Crew Female Adult Yes 20
您可能需要尝试:
SQL_Table %>%
arrange_at(vars(1:iss_age, dur))
但是您的数据很难读取。
您还可以使用one_of
选择可能存在或可能不存在的变量。