按字母顺序按组对数据框重新排序
Reorder dataframe by groups in alphabetical order
我有这个数据框:
df1
speaker action phase
31 <NA> are only four= <NA>
33 ID1-P ((m: r hand holds up three fingers ifo face)) B
35 ID1-G ((m: r hand holds up three fingers ifo face)) A
37 ID1-P ((m: r hand holds up three fingers ifo face)) D
39 <NA> (0.215) <NA>
41 ID2-A =mhm, <NA>
47 ID1-P ((m: r hand holds up three fingers ifo face)) E
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up four fingers)) A
78 ID1-P ((m: r hand holds up four fingers)) B
80 ID1-A =we are !four! <NA>
82 ID1-P ((m: r hand holds up four fingers)) C
84 ID1-P ((m: r hand holds up four fingers)) E
86 <NA> (0.031) <NA>
我想重新排列它,使 phase
不是 NA
的所有行 (i) 立即放在一起并且 (ii) 按字母顺序排列。
预期结果:
df1[c(1,3,2,4,7,5:6,8:9,10:11,13:14,12,15),]
speaker action 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>
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up four fingers)) A
78 ID1-P ((m: r hand holds up four fingers)) B
82 ID1-P ((m: r hand holds up four fingers)) C
84 ID1-P ((m: r hand holds up four fingers)) E
80 ID1-A =we are !four! <NA>
86 <NA> (0.031) <NA>
我试过 df %>% arrange(phase)
和 df[order(df$phase),]
都无济于事。非常感谢您的帮助!
可重现的数据:
dput(df[c(1:6,9:10,23:29),])
structure(list(speaker = c(NA, "ID1-P", "ID1-G", "ID1-P", NA,
"ID2-A", "ID1-P", NA, "ID2-A", "ID1-G", "ID1-P", "ID1-A", "ID1-P",
"ID1-P", NA), action = 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,", "((m: r hand holds up three fingers ifo face))",
"(0.282)", "<no: yeah: it 's:>=", "((m: r hand holds up four fingers))",
"((m: r hand holds up four fingers))", "=we are !four!", "((m: r hand holds up four fingers))",
"((m: r hand holds up four fingers))", "(0.031)"), phase = c(NA,
"B", "A", "D", NA, NA, "E", NA, NA, "A", "B", NA, "C", "E", NA
)), row.names = c(31L, 33L, 35L, 37L, 39L, 41L, 47L, 49L, 74L,
76L, 78L, 80L, 82L, 84L, 86L), class = "data.frame")
您可以将 arrange
与 match
一起使用:
library(dplyr)
df %>% arrange(match(action, unique(action)), phase)
或者在 base R 中使用 order
:
df[with(df, order(match(action, unique(action)), phase)), ]
# speaker action 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>
#49 <NA> (0.282) <NA>
#74 ID2-A <no: yeah: it 's:>= <NA>
#76 ID1-G ((m: r hand holds up four fingers)) A
#78 ID1-P ((m: r hand holds up four fingers)) B
#82 ID1-P ((m: r hand holds up four fingers)) C
#84 ID1-P ((m: r hand holds up four fingers)) E
#80 ID1-A =we are !four! <NA>
#86 <NA> (0.031) <NA>
我有这个数据框:
df1
speaker action phase
31 <NA> are only four= <NA>
33 ID1-P ((m: r hand holds up three fingers ifo face)) B
35 ID1-G ((m: r hand holds up three fingers ifo face)) A
37 ID1-P ((m: r hand holds up three fingers ifo face)) D
39 <NA> (0.215) <NA>
41 ID2-A =mhm, <NA>
47 ID1-P ((m: r hand holds up three fingers ifo face)) E
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up four fingers)) A
78 ID1-P ((m: r hand holds up four fingers)) B
80 ID1-A =we are !four! <NA>
82 ID1-P ((m: r hand holds up four fingers)) C
84 ID1-P ((m: r hand holds up four fingers)) E
86 <NA> (0.031) <NA>
我想重新排列它,使 phase
不是 NA
的所有行 (i) 立即放在一起并且 (ii) 按字母顺序排列。
预期结果:
df1[c(1,3,2,4,7,5:6,8:9,10:11,13:14,12,15),]
speaker action 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>
49 <NA> (0.282) <NA>
74 ID2-A <no: yeah: it 's:>= <NA>
76 ID1-G ((m: r hand holds up four fingers)) A
78 ID1-P ((m: r hand holds up four fingers)) B
82 ID1-P ((m: r hand holds up four fingers)) C
84 ID1-P ((m: r hand holds up four fingers)) E
80 ID1-A =we are !four! <NA>
86 <NA> (0.031) <NA>
我试过 df %>% arrange(phase)
和 df[order(df$phase),]
都无济于事。非常感谢您的帮助!
可重现的数据:
dput(df[c(1:6,9:10,23:29),])
structure(list(speaker = c(NA, "ID1-P", "ID1-G", "ID1-P", NA,
"ID2-A", "ID1-P", NA, "ID2-A", "ID1-G", "ID1-P", "ID1-A", "ID1-P",
"ID1-P", NA), action = 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,", "((m: r hand holds up three fingers ifo face))",
"(0.282)", "<no: yeah: it 's:>=", "((m: r hand holds up four fingers))",
"((m: r hand holds up four fingers))", "=we are !four!", "((m: r hand holds up four fingers))",
"((m: r hand holds up four fingers))", "(0.031)"), phase = c(NA,
"B", "A", "D", NA, NA, "E", NA, NA, "A", "B", NA, "C", "E", NA
)), row.names = c(31L, 33L, 35L, 37L, 39L, 41L, 47L, 49L, 74L,
76L, 78L, 80L, 82L, 84L, 86L), class = "data.frame")
您可以将 arrange
与 match
一起使用:
library(dplyr)
df %>% arrange(match(action, unique(action)), phase)
或者在 base R 中使用 order
:
df[with(df, order(match(action, unique(action)), phase)), ]
# speaker action 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>
#49 <NA> (0.282) <NA>
#74 ID2-A <no: yeah: it 's:>= <NA>
#76 ID1-G ((m: r hand holds up four fingers)) A
#78 ID1-P ((m: r hand holds up four fingers)) B
#82 ID1-P ((m: r hand holds up four fingers)) C
#84 ID1-P ((m: r hand holds up four fingers)) E
#80 ID1-A =we are !four! <NA>
#86 <NA> (0.031) <NA>