-1

I have this dataframe:

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>

I would like to rearrange it such that all rows where phase is NOT NA are (i) immediately together and (ii) alphabetically ordered.

Expected outcome:

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>

I've tried df %>% arrange(phase) and df[order(df$phase),]to no avail. Help with this is much appreciated!

Reproducible data:

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")
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34

1 Answers1

3

You can use arrange with match :

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

Or in base R using 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>
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213