R 按字母顺序按组对数据帧重新排序
我有这个数据框:R 按字母顺序按组对数据帧重新排序,r,grouping,R,Grouping,我有这个数据框: 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 thre
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>
我尝试了df%>%arrangephase和df[orderdf$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 with match:
或在基本R中使用顺序:
你如何决定如何分组?我的意思是,为什么A B D E在一起,而其余部分是分开的?对于每个组,action列中的值都与您所知道的相同。我花了很长时间来构思这个问题并得到答案!
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")
library(dplyr)
df %>% arrange(match(action, unique(action)), phase)
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>