根据组名同时对 2 列进行排序
Sort 2 columns simultaneously based on the group name
数据集
> read.delim("df.tsv")
col1 col2 group
1 3 2 aa
2 1 1 aa
3 4 1 aa
4 4 3 aa
5 5 3 ab
6 3 2 ab
7 4 1 ab
8 2 4 ab
9 4 2 ba
10 1 4 ba
11 3 1 ba
12 4 3 ba
13 4 2 bb
14 2 3 bb
15 3 1 bb
16 1 2 bb
我想按以下方式排序 4 组中的列 col1 和 col2 :
- 如果组名中的1st字符为“a”,排序col1 以 descending 方式,ascending 如果它是 "b
- 如果组名中的2nd字符为“a”,排序col2 以 descending 方式,ascending 如果它是 "b
- 重要的是,我希望两列都同时排序,即如果该组是“aa”,该组的排序应如下所示:
col1 col2 group
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
...
这可以通过例如a "one row at a time" approach,首先是 col1,然后是 col2,每行交替。
当前代码和输出
library(dplyr)
read.delim("df.tsv") %>%
group_by(group) %>%
arrange(ifelse(substr(group, 1,1) == "a", desc(col1), col1), # if first character in group name is "a", sort col1 in a descending manner, and ascending if it's "b"
ifelse(substr(group, 2,2) == "a", desc(col2), col2), # if second character in group name is also "a", sort also col2 in a descending manner, and ascending if it's "b"
.by_group = TRUE)
col1 col2 group
1 4 3 aa
2 4 1 aa
3 3 2 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 3 1 ba
11 4 3 ba
12 4 2 ba
13 1 2 bb
14 2 3 bb
15 3 1 bb
16 4 2 bb
但是,这不满足第三个标准,即“一次同时排序一行”。
期望的输出
col1 col2 group
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 4 3 ba
11 3 1 ba
12 4 2 ba
13 1 2 bb
14 3 1 bb
15 2 3 bb
16 4 2 bb
编辑
实际上有几个答案可以完成建议的任务,所以我认为决胜局可能是该算法在要排序的列数方面很灵活,例如3:
col1 col2 col3 group
3 2 4 aaa
1 1 2 aaa
4 1 4 aaa
4 3 1 aaa
5 3 3 aab
3 2 2 aab
4 1 1 aab
2 4 1 aab
4 2 3 aba
1 4 3 aba
3 1 2 aba
4 3 3 aba
3 2 4 abb
1 1 2 abb
4 1 4 abb
4 3 1 abb
4 2 1 baa
2 3 2 baa
3 1 2 baa
1 2 1 baa
5 3 3 bab
3 2 2 bab
4 1 1 bab
2 4 1 bab
4 2 3 bba
1 4 3 bba
3 1 2 bba
4 3 3 bba
4 2 1 bbb
2 3 2 bbb
3 1 2 bbb
1 2 1 bbb
输出应该是
col1 col2 col3 group
4 3 1 aaa
3 2 4 aaa
4 1 4 aaa
1 1 2 aaa
5 3 3 aab
2 4 1 aab
4 1 1 aab
3 2 2 aab
4 2 3 aba
3 1 2 aba
4 3 3 aba
1 4 3 aba
4 1 4 abb
1 1 2 abb
4 3 1 abb
3 2 4 abb
1 2 1 baa
2 3 2 baa
3 1 2 baa
4 2 1 baa
2 4 1 bab
5 3 3 bab
4 1 1 bab
3 2 2 bab
1 4 3 bba
3 1 2 bba
4 2 3 bba
4 3 3 bba
1 2 1 bbb
3 1 2 bbb
4 2 1 bbb
2 3 2 bbb
目前,当包含 3 列或更多列时,建议的 2 种解决方案不起作用,它们仅基于 2 列进行排序。
编辑 2
如果例如group=='aba',该组的第一行应该是包含col1中最高值的那一行;第二行包含 col2 中的(剩余)最低值;第 3 行包含 col3 中的(剩余)最高值,第 4 行是剩余的行。但是,这应该是灵活的,以允许每组超过 4 行,在这种情况下,第 4 行应该是包含 col1 中(剩余的)最高值的行;第 5 行应该是包含 col2 中(剩余的)最低值的那一行;等等
更多详情
示例:对于 'aba' 组的第 2 行,如果 col2 中最低(剩余)值的两行之间存在平局,例如
row-a 3 1 4 aba
row-b 2 1 4 aba
(注意两行的 col2 中都有一个 1),理想情况下选择的第二行将是 row-a,因为 col1 必须以降序方式排序('a')这个组,3>2,对于 col3 4==4 无论如何。
如果相反
row-a 3 1 4 aba
row-b 2 1 5 aba
让优先顺序为 col3>col2>col1,因为循环为 col1>col2>col3... 所以第二行将是行 b,因为 5>4。
所以概括地说,如果有 5 列并且组是 'aabaa',并且在 2 行之间选择第 3 行有平局:
row-a 3 2 1 3 3 aabaa
row-b 5 4 1 4 2 aabaa
(col3 == 1 in both),然后到 select 的那个将是 row-a 因为 col5 3>2。如果相反
row-a--> 3 2 1 3 3
row-b--> 5 4 1 4 3
(两者均为 col5==3),然后选择 row-b,因为 col4 4>3.
我可以告诉你答案,但我无法为它编写完整的 r
代码,因为我不知道 r
,我希望有人可以编辑我的代码以获得完整的答案.
假设两种排序都是升序的(您可以将其概括为您的情况)
idx1=order(col1)
idx2=order(col2[idx1])
return col1[idx1[idx2]], col2[idx1[idx2]]
再想一想,我想我可以把那个选项传递给你。您现在可以指定任何您想要的循环方式。
alt_order <- function(..., type, cyc) {
cols <- unname(list(...))
stopifnot(
# sanity checks; you may skip if you think they are unnecessary
length(unique(lengths(cols))) == 1L,
length(cols) == length(type),
all(unlist(type) %in% c(1L, -1L))
)
cols <- mapply(`*`, cols, type, SIMPLIFY = FALSE)
out <- integer(length(cols[[1L]]))
this <- cols
for (i in seq_along(out)) {
out[[i]] <- do.call(order, this)[[1L]]
cols <- lapply(cols, `is.na<-`, out[[i]])
this <- cols[cyc(i)]
}
out
}
cyc
应该是一个接受单个整数作为输入的函数,returns 应该是一个整数向量。例如,如果你有 3 列并且你想复制我在下面评论中描述的 rev
循环行为,你可以这样做
mycyc <- function(i) list(1:3, 3:1)[[(i - 1) %% 2L + 1L]]
df %>% group_by(group) %>% slice(alt_order(col1, col2, col3, type = ab2sign(group), cyc = mycyc))
嗯,也许一个效率不高但简单的解决方案是连续对两列进行排序,每次交换主列,并排出第一个元素,直到没有元素需要排序。这是函数。
alt_order <- function(..., type) {
cols <- unname(list(...))
stopifnot(
# sanity checks; you may skip if you think they are unnecessary
length(unique(lengths(cols))) == 1L,
length(cols) == length(type),
all(unlist(type) %in% c(1L, -1L))
)
cols <- mapply(`*`, cols, type, SIMPLIFY = FALSE)
out <- integer(length(cols[[1L]]))
for (i in seq_along(out)) {
out[[i]] <- do.call(order, cols)[[1L]]
cols <- rev(lapply(cols, `is.na<-`, out[[i]]))
}
out
}
我们给 NA
赋值以释放它们,因为 NA
将按升序排到最后。 type
应为 1 或 -1,用于简化我们想要施加的顺序,因为 c(1,2,3)
的降序与 -1 * c(1,2,3)
的升序相同。我们还需要一个辅助函数,如下所示将您的 group
s 转换为 1 和 -1
ab2sign <- function(x) {
out <- data.table::transpose(strsplit(x, "", fixed = TRUE))
lapply(out, function(x) 2L * (x == "b") - 1L)
}
现在我们可以应用它们了
df %>% group_by(group) %>% slice(alt_order(col1, col2, type = ab2sign(group)))
输出
# A tibble: 16 x 3
# Groups: group [4]
col1 col2 group
<int> <int> <chr>
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 4 3 ba
11 3 1 ba
12 4 2 ba
13 1 2 bb
14 3 1 bb
15 2 3 bb
16 4 2 bb
我希望在赏金到期前看到更高效(可能是矢量化)的解决方案。
更新
以下选项可能适用于一般情况,即超过 2 列:
f <- function(.) {
col <- .[-length(.)] * (2 * (t(list2DF(strsplit(.$group, ""))) == "b") - 1)
r <- data.frame()
while (nrow(.)) {
p <- do.call(order, col[(seq_along(col) + nrow(r) - 1) %% length(col) + 1])[1]
r <- rbind(r, .[p, ])
col <- col[-p, ]
. <- .[-p, ]
}
r
}
df %>%
group_by(group) %>%
do(f(.)) %>%
ungroup()
这给出了
col1 col2 col3 group
<int> <int> <int> <chr>
1 4 3 1 aaa
2 3 2 4 aaa
3 4 1 4 aaa
4 1 1 2 aaa
5 5 3 3 aab
6 2 4 1 aab
7 4 1 1 aab
8 3 2 2 aab
9 4 2 3 aba
10 3 1 2 aba
# ... with 22 more rows
这是一个使用动态规划的选项(但可能效率不高)
f <- function(.) {
col <- with(., data.frame(col1, col2) * (2 * (t(list2DF(strsplit(.$group, ""))) == "b") - 1))
r <- data.frame()
while (nrow(.)) {
p <- do.call(order, ifelse(nrow(r) %% 2, rev, I)(col))[1]
r <- rbind(r, .[p, ])
col <- col[-p,]
. <- .[-p, ]
}
r
}
df %>%
group_by(group) %>%
do(f(.)) %>%
ungroup()
这给出了
# A tibble: 16 x 3
col1 col2 group
<int> <int> <chr>
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 4 3 ba
11 3 1 ba
12 4 2 ba
13 1 2 bb
14 3 1 bb
15 2 3 bb
16 4 2 bb
数据集
> read.delim("df.tsv")
col1 col2 group
1 3 2 aa
2 1 1 aa
3 4 1 aa
4 4 3 aa
5 5 3 ab
6 3 2 ab
7 4 1 ab
8 2 4 ab
9 4 2 ba
10 1 4 ba
11 3 1 ba
12 4 3 ba
13 4 2 bb
14 2 3 bb
15 3 1 bb
16 1 2 bb
我想按以下方式排序 4 组中的列 col1 和 col2 :
- 如果组名中的1st字符为“a”,排序col1 以 descending 方式,ascending 如果它是 "b
- 如果组名中的2nd字符为“a”,排序col2 以 descending 方式,ascending 如果它是 "b
- 重要的是,我希望两列都同时排序,即如果该组是“aa”,该组的排序应如下所示:
col1 col2 group
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
...
这可以通过例如a "one row at a time" approach,首先是 col1,然后是 col2,每行交替。
当前代码和输出
library(dplyr)
read.delim("df.tsv") %>%
group_by(group) %>%
arrange(ifelse(substr(group, 1,1) == "a", desc(col1), col1), # if first character in group name is "a", sort col1 in a descending manner, and ascending if it's "b"
ifelse(substr(group, 2,2) == "a", desc(col2), col2), # if second character in group name is also "a", sort also col2 in a descending manner, and ascending if it's "b"
.by_group = TRUE)
col1 col2 group
1 4 3 aa
2 4 1 aa
3 3 2 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 3 1 ba
11 4 3 ba
12 4 2 ba
13 1 2 bb
14 2 3 bb
15 3 1 bb
16 4 2 bb
但是,这不满足第三个标准,即“一次同时排序一行”。
期望的输出
col1 col2 group
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 4 3 ba
11 3 1 ba
12 4 2 ba
13 1 2 bb
14 3 1 bb
15 2 3 bb
16 4 2 bb
编辑
实际上有几个答案可以完成建议的任务,所以我认为决胜局可能是该算法在要排序的列数方面很灵活,例如3:
col1 col2 col3 group
3 2 4 aaa
1 1 2 aaa
4 1 4 aaa
4 3 1 aaa
5 3 3 aab
3 2 2 aab
4 1 1 aab
2 4 1 aab
4 2 3 aba
1 4 3 aba
3 1 2 aba
4 3 3 aba
3 2 4 abb
1 1 2 abb
4 1 4 abb
4 3 1 abb
4 2 1 baa
2 3 2 baa
3 1 2 baa
1 2 1 baa
5 3 3 bab
3 2 2 bab
4 1 1 bab
2 4 1 bab
4 2 3 bba
1 4 3 bba
3 1 2 bba
4 3 3 bba
4 2 1 bbb
2 3 2 bbb
3 1 2 bbb
1 2 1 bbb
输出应该是
col1 col2 col3 group
4 3 1 aaa
3 2 4 aaa
4 1 4 aaa
1 1 2 aaa
5 3 3 aab
2 4 1 aab
4 1 1 aab
3 2 2 aab
4 2 3 aba
3 1 2 aba
4 3 3 aba
1 4 3 aba
4 1 4 abb
1 1 2 abb
4 3 1 abb
3 2 4 abb
1 2 1 baa
2 3 2 baa
3 1 2 baa
4 2 1 baa
2 4 1 bab
5 3 3 bab
4 1 1 bab
3 2 2 bab
1 4 3 bba
3 1 2 bba
4 2 3 bba
4 3 3 bba
1 2 1 bbb
3 1 2 bbb
4 2 1 bbb
2 3 2 bbb
目前,当包含 3 列或更多列时,建议的 2 种解决方案不起作用,它们仅基于 2 列进行排序。
编辑 2
如果例如group=='aba',该组的第一行应该是包含col1中最高值的那一行;第二行包含 col2 中的(剩余)最低值;第 3 行包含 col3 中的(剩余)最高值,第 4 行是剩余的行。但是,这应该是灵活的,以允许每组超过 4 行,在这种情况下,第 4 行应该是包含 col1 中(剩余的)最高值的行;第 5 行应该是包含 col2 中(剩余的)最低值的那一行;等等
更多详情
示例:对于 'aba' 组的第 2 行,如果 col2 中最低(剩余)值的两行之间存在平局,例如
row-a 3 1 4 aba
row-b 2 1 4 aba
(注意两行的 col2 中都有一个 1),理想情况下选择的第二行将是 row-a,因为 col1 必须以降序方式排序('a')这个组,3>2,对于 col3 4==4 无论如何。
如果相反
row-a 3 1 4 aba
row-b 2 1 5 aba
让优先顺序为 col3>col2>col1,因为循环为 col1>col2>col3... 所以第二行将是行 b,因为 5>4。
所以概括地说,如果有 5 列并且组是 'aabaa',并且在 2 行之间选择第 3 行有平局:
row-a 3 2 1 3 3 aabaa
row-b 5 4 1 4 2 aabaa
(col3 == 1 in both),然后到 select 的那个将是 row-a 因为 col5 3>2。如果相反
row-a--> 3 2 1 3 3
row-b--> 5 4 1 4 3
(两者均为 col5==3),然后选择 row-b,因为 col4 4>3.
我可以告诉你答案,但我无法为它编写完整的 r
代码,因为我不知道 r
,我希望有人可以编辑我的代码以获得完整的答案.
假设两种排序都是升序的(您可以将其概括为您的情况)
idx1=order(col1)
idx2=order(col2[idx1])
return col1[idx1[idx2]], col2[idx1[idx2]]
再想一想,我想我可以把那个选项传递给你。您现在可以指定任何您想要的循环方式。
alt_order <- function(..., type, cyc) {
cols <- unname(list(...))
stopifnot(
# sanity checks; you may skip if you think they are unnecessary
length(unique(lengths(cols))) == 1L,
length(cols) == length(type),
all(unlist(type) %in% c(1L, -1L))
)
cols <- mapply(`*`, cols, type, SIMPLIFY = FALSE)
out <- integer(length(cols[[1L]]))
this <- cols
for (i in seq_along(out)) {
out[[i]] <- do.call(order, this)[[1L]]
cols <- lapply(cols, `is.na<-`, out[[i]])
this <- cols[cyc(i)]
}
out
}
cyc
应该是一个接受单个整数作为输入的函数,returns 应该是一个整数向量。例如,如果你有 3 列并且你想复制我在下面评论中描述的 rev
循环行为,你可以这样做
mycyc <- function(i) list(1:3, 3:1)[[(i - 1) %% 2L + 1L]]
df %>% group_by(group) %>% slice(alt_order(col1, col2, col3, type = ab2sign(group), cyc = mycyc))
嗯,也许一个效率不高但简单的解决方案是连续对两列进行排序,每次交换主列,并排出第一个元素,直到没有元素需要排序。这是函数。
alt_order <- function(..., type) {
cols <- unname(list(...))
stopifnot(
# sanity checks; you may skip if you think they are unnecessary
length(unique(lengths(cols))) == 1L,
length(cols) == length(type),
all(unlist(type) %in% c(1L, -1L))
)
cols <- mapply(`*`, cols, type, SIMPLIFY = FALSE)
out <- integer(length(cols[[1L]]))
for (i in seq_along(out)) {
out[[i]] <- do.call(order, cols)[[1L]]
cols <- rev(lapply(cols, `is.na<-`, out[[i]]))
}
out
}
我们给 NA
赋值以释放它们,因为 NA
将按升序排到最后。 type
应为 1 或 -1,用于简化我们想要施加的顺序,因为 c(1,2,3)
的降序与 -1 * c(1,2,3)
的升序相同。我们还需要一个辅助函数,如下所示将您的 group
s 转换为 1 和 -1
ab2sign <- function(x) {
out <- data.table::transpose(strsplit(x, "", fixed = TRUE))
lapply(out, function(x) 2L * (x == "b") - 1L)
}
现在我们可以应用它们了
df %>% group_by(group) %>% slice(alt_order(col1, col2, type = ab2sign(group)))
输出
# A tibble: 16 x 3
# Groups: group [4]
col1 col2 group
<int> <int> <chr>
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 4 3 ba
11 3 1 ba
12 4 2 ba
13 1 2 bb
14 3 1 bb
15 2 3 bb
16 4 2 bb
我希望在赏金到期前看到更高效(可能是矢量化)的解决方案。
更新
以下选项可能适用于一般情况,即超过 2 列:
f <- function(.) {
col <- .[-length(.)] * (2 * (t(list2DF(strsplit(.$group, ""))) == "b") - 1)
r <- data.frame()
while (nrow(.)) {
p <- do.call(order, col[(seq_along(col) + nrow(r) - 1) %% length(col) + 1])[1]
r <- rbind(r, .[p, ])
col <- col[-p, ]
. <- .[-p, ]
}
r
}
df %>%
group_by(group) %>%
do(f(.)) %>%
ungroup()
这给出了
col1 col2 col3 group
<int> <int> <int> <chr>
1 4 3 1 aaa
2 3 2 4 aaa
3 4 1 4 aaa
4 1 1 2 aaa
5 5 3 3 aab
6 2 4 1 aab
7 4 1 1 aab
8 3 2 2 aab
9 4 2 3 aba
10 3 1 2 aba
# ... with 22 more rows
这是一个使用动态规划的选项(但可能效率不高)
f <- function(.) {
col <- with(., data.frame(col1, col2) * (2 * (t(list2DF(strsplit(.$group, ""))) == "b") - 1))
r <- data.frame()
while (nrow(.)) {
p <- do.call(order, ifelse(nrow(r) %% 2, rev, I)(col))[1]
r <- rbind(r, .[p, ])
col <- col[-p,]
. <- .[-p, ]
}
r
}
df %>%
group_by(group) %>%
do(f(.)) %>%
ungroup()
这给出了
# A tibble: 16 x 3
col1 col2 group
<int> <int> <chr>
1 4 3 aa
2 3 2 aa
3 4 1 aa
4 1 1 aa
5 5 3 ab
6 4 1 ab
7 3 2 ab
8 2 4 ab
9 1 4 ba
10 4 3 ba
11 3 1 ba
12 4 2 ba
13 1 2 bb
14 3 1 bb
15 2 3 bb
16 4 2 bb