>source

이 데이터 프레임이 있습니다.

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")


  • 답변 # 1

    당신이 사용할 수있는 arrangematch :

    library(dplyr)
    df %>% arrange(match(action, unique(action)), phase)
    
    

    또는 기본 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>
    
    

관련 자료

  • 이전 jquery - 이 경로에는 POST 방법이 지원되지 않습니다
  • 다음 ios - indexPathrow가 0이 아닌 1에서 시작하는 이유