尽管组之间共享值,如何按不同组的字母顺序重新排列行?
How to re-arrange rows alphabetically by distinct groups despite shared values between groups?
我有这个数据框:
df
speaker actionx phase
31 <NA> are only four= <NA>
33 ID1-P ((m: r hand holds up three fingers ifo face)) B # group 1
35 ID1-G ((m: r hand holds up three fingers ifo face)) A # group 1
37 ID1-P ((m: r hand holds up three fingers ifo face)) D # group 1
39 <NA> (0.215) <NA>
41 ID2-A =mhm, <NA>
43 <NA> (0.270) <NA>
45 ID1-A So:: if you take a leave of absence we are going going to be three= <NA>
47 ID1-P ((m: r hand holds up three fingers ifo face)) E # group 1
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up three fingers ifo face)) A # group 2
78 ID1-P ((m: r hand holds up three fingers ifo face)) B # group 2
80 ID1-A =we are !four! <NA>
82 ID1-P ((m: r hand holds up three fingers ifo face)) C # group 2
84 ID1-P ((m: r hand holds up three fingers ifo face)) E # group 2
86 <NA> (0.031) <NA>
我想重新排列行,使 phase
列中的组按字母顺序排列并彼此紧邻。这些组可通过 (i) 字母 A
到 E
和 (ii) actionx
列中的值相同这一事实来识别。
多亏了 SO 成员的建议,我知道如果所有组之间都有 不同的 actionx
值,如何重新排序行,即:
df <- df[order(match(df$actionx, unique(df$actionx)), df$phase), ]
# or:
library(dplyr)
df <- df %>% arrange(match(actionx, unique(actionx)), phase)
但是,有时,这些组之间具有 相同的 actionx
值;例如,在 df
中,group 1
和 group 2
在 actionx
.
列中共享值 ((m: r hand holds up three fingers ifo face))
如何在 actionx
中的值相同的情况下按字母顺序按组重新排列行以实现此 预期结果? (请注意,字母 A
到 E
每组不得出现多次。)
df[c(1,3,2,4,9,5:8,10:13,15:16,14,17),]
speaker actionx phase
31 <NA> are only four= <NA>
35 ID1-G ((m: r hand holds up three fingers ifo face)) A
33 ID1-P ((m: r hand holds up three fingers ifo face)) B
37 ID1-P ((m: r hand holds up three fingers ifo face)) D
47 ID1-P ((m: r hand holds up three fingers ifo face)) E
39 <NA> (0.215) <NA>
41 ID2-A =mhm, <NA>
43 <NA> (0.270) <NA>
45 ID1-A So:: if you take a leave of absence we are going going to be three= <NA>
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up three fingers ifo face)) A
78 ID1-P ((m: r hand holds up three fingers ifo face)) B
82 ID1-P ((m: r hand holds up three fingers ifo face)) C
84 ID1-P ((m: r hand holds up three fingers ifo face)) E
80 ID1-A =we are !four! <NA>
86 <NA> (0.031) <NA>
可重现数据:
df <- dput(t[c(17:26, 39:45), c(2,7,6)])
structure(list(speaker = c(NA, "ID1-P", "ID1-G", "ID1-P", NA,
"ID2-A", NA, "ID1-A", "ID1-P", NA, "ID2-A", "ID1-G", "ID1-P",
"ID1-A", "ID1-P", "ID1-P", NA), actionx = c("are only four=",
"((m: r hand holds up three fingers ifo face))", "((m: r hand holds up three fingers ifo face))",
"((m: r hand holds up three fingers ifo face))", "(0.215)", "=mhm,",
"(0.270)", "So:: if you take a leave of absence we are going going to be three=",
"((m: r hand holds up three fingers ifo face))", "(0.282)", "<no: yeah: it 's:>=",
"((m: r hand holds up three fingers ifo face))", "((m: r hand holds up three fingers ifo face))",
"=we are !four!", "((m: r hand holds up three fingers ifo face))",
"((m: r hand holds up three fingers ifo face))", "(0.031)"),
phase = c(NA, "B", "A", "D", NA, NA, NA, NA, "E", NA, NA,
"A", "B", NA, "C", "E", NA)), row.names = c(31L, 33L, 35L,
37L, 39L, 41L, 43L, 45L, 47L, 49L, 74L, 76L, 78L, 80L, 82L, 84L,
86L), class = "data.frame")
你需要两步arrange()
:
library(dplyr)
df %>%
arrange(data.table::rleid(!is.na(phase)),
phase) %>%
arrange(cumsum(coalesce(phase == "A", F)), ## if phase = "A", jump to the next group
match(actionx, unique(actionx)))
# speaker actionx phase
# 1 <NA> are only four= <NA>
# 2 ID1-G ((m: r hand holds up three fingers ifo face)) A
# 3 ID1-P ((m: r hand holds up three fingers ifo face)) B
# 4 ID1-P ((m: r hand holds up three fingers ifo face)) D
# 5 ID1-P ((m: r hand holds up three fingers ifo face)) E
# 6 <NA> (0.215) <NA>
# 7 ID2-A =mhm, <NA>
# 8 <NA> (0.270) <NA>
# 9 ID1-A So:: if you take a leave of absence we are going going to be three= <NA>
# 10 <NA> (0.282) <NA>
# 11 ID2-A <no: yeah: it 's:>= <NA>
# 12 ID1-G ((m: r hand holds up three fingers ifo face)) A
# 13 ID1-P ((m: r hand holds up three fingers ifo face)) B
# 14 ID1-P ((m: r hand holds up three fingers ifo face)) C
# 15 ID1-P ((m: r hand holds up three fingers ifo face)) E
# 16 ID1-A =we are !four! <NA>
# 17 <NA> (0.031) <NA>
注意: 不要将两个arrange()
合并在一起,因为phase
在第二个 arrange()
是新订单。
我有这个数据框:
df
speaker actionx phase
31 <NA> are only four= <NA>
33 ID1-P ((m: r hand holds up three fingers ifo face)) B # group 1
35 ID1-G ((m: r hand holds up three fingers ifo face)) A # group 1
37 ID1-P ((m: r hand holds up three fingers ifo face)) D # group 1
39 <NA> (0.215) <NA>
41 ID2-A =mhm, <NA>
43 <NA> (0.270) <NA>
45 ID1-A So:: if you take a leave of absence we are going going to be three= <NA>
47 ID1-P ((m: r hand holds up three fingers ifo face)) E # group 1
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up three fingers ifo face)) A # group 2
78 ID1-P ((m: r hand holds up three fingers ifo face)) B # group 2
80 ID1-A =we are !four! <NA>
82 ID1-P ((m: r hand holds up three fingers ifo face)) C # group 2
84 ID1-P ((m: r hand holds up three fingers ifo face)) E # group 2
86 <NA> (0.031) <NA>
我想重新排列行,使 phase
列中的组按字母顺序排列并彼此紧邻。这些组可通过 (i) 字母 A
到 E
和 (ii) actionx
列中的值相同这一事实来识别。
多亏了 SO 成员的建议,我知道如果所有组之间都有 不同的 actionx
值,如何重新排序行,即:
df <- df[order(match(df$actionx, unique(df$actionx)), df$phase), ]
# or:
library(dplyr)
df <- df %>% arrange(match(actionx, unique(actionx)), phase)
但是,有时,这些组之间具有 相同的 actionx
值;例如,在 df
中,group 1
和 group 2
在 actionx
.
((m: r hand holds up three fingers ifo face))
如何在 actionx
中的值相同的情况下按字母顺序按组重新排列行以实现此 预期结果? (请注意,字母 A
到 E
每组不得出现多次。)
df[c(1,3,2,4,9,5:8,10:13,15:16,14,17),]
speaker actionx phase
31 <NA> are only four= <NA>
35 ID1-G ((m: r hand holds up three fingers ifo face)) A
33 ID1-P ((m: r hand holds up three fingers ifo face)) B
37 ID1-P ((m: r hand holds up three fingers ifo face)) D
47 ID1-P ((m: r hand holds up three fingers ifo face)) E
39 <NA> (0.215) <NA>
41 ID2-A =mhm, <NA>
43 <NA> (0.270) <NA>
45 ID1-A So:: if you take a leave of absence we are going going to be three= <NA>
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up three fingers ifo face)) A
78 ID1-P ((m: r hand holds up three fingers ifo face)) B
82 ID1-P ((m: r hand holds up three fingers ifo face)) C
84 ID1-P ((m: r hand holds up three fingers ifo face)) E
80 ID1-A =we are !four! <NA>
86 <NA> (0.031) <NA>
可重现数据:
df <- dput(t[c(17:26, 39:45), c(2,7,6)])
structure(list(speaker = c(NA, "ID1-P", "ID1-G", "ID1-P", NA,
"ID2-A", NA, "ID1-A", "ID1-P", NA, "ID2-A", "ID1-G", "ID1-P",
"ID1-A", "ID1-P", "ID1-P", NA), actionx = c("are only four=",
"((m: r hand holds up three fingers ifo face))", "((m: r hand holds up three fingers ifo face))",
"((m: r hand holds up three fingers ifo face))", "(0.215)", "=mhm,",
"(0.270)", "So:: if you take a leave of absence we are going going to be three=",
"((m: r hand holds up three fingers ifo face))", "(0.282)", "<no: yeah: it 's:>=",
"((m: r hand holds up three fingers ifo face))", "((m: r hand holds up three fingers ifo face))",
"=we are !four!", "((m: r hand holds up three fingers ifo face))",
"((m: r hand holds up three fingers ifo face))", "(0.031)"),
phase = c(NA, "B", "A", "D", NA, NA, NA, NA, "E", NA, NA,
"A", "B", NA, "C", "E", NA)), row.names = c(31L, 33L, 35L,
37L, 39L, 41L, 43L, 45L, 47L, 49L, 74L, 76L, 78L, 80L, 82L, 84L,
86L), class = "data.frame")
你需要两步arrange()
:
library(dplyr)
df %>%
arrange(data.table::rleid(!is.na(phase)),
phase) %>%
arrange(cumsum(coalesce(phase == "A", F)), ## if phase = "A", jump to the next group
match(actionx, unique(actionx)))
# speaker actionx phase
# 1 <NA> are only four= <NA>
# 2 ID1-G ((m: r hand holds up three fingers ifo face)) A
# 3 ID1-P ((m: r hand holds up three fingers ifo face)) B
# 4 ID1-P ((m: r hand holds up three fingers ifo face)) D
# 5 ID1-P ((m: r hand holds up three fingers ifo face)) E
# 6 <NA> (0.215) <NA>
# 7 ID2-A =mhm, <NA>
# 8 <NA> (0.270) <NA>
# 9 ID1-A So:: if you take a leave of absence we are going going to be three= <NA>
# 10 <NA> (0.282) <NA>
# 11 ID2-A <no: yeah: it 's:>= <NA>
# 12 ID1-G ((m: r hand holds up three fingers ifo face)) A
# 13 ID1-P ((m: r hand holds up three fingers ifo face)) B
# 14 ID1-P ((m: r hand holds up three fingers ifo face)) C
# 15 ID1-P ((m: r hand holds up three fingers ifo face)) E
# 16 ID1-A =we are !four! <NA>
# 17 <NA> (0.031) <NA>
注意: 不要将两个arrange()
合并在一起,因为phase
在第二个 arrange()
是新订单。