keeping the original order of data after split() in R

Viewed 758

In the following R code, I split a data.frame by a column, a string variable called study.name.

But split alphabetically re-orders the original data.frame. In BASE R, is it possible to keep the original order of data after splitting?

D <- read.csv("https://raw.githubusercontent.com/izeh/i/master/k.csv", h = T) # data.frame 
m <- split(D, D$study.name)
1 Answers

We can split by factor converted 'study.name', where the levels are specified as the unique elements of the column and unique returns the values in the same order of occurrence of unique elements

split(D, factor(D$study.name, levels = unique(D$study.name)))

if we need to delete the NA elements, subset the data before the split

D1 <- subset(D, !(is.na(study.name)| study.name == ""))
split(D1, factor(D1$study.name, levels = unique(D1$study.name)))
#$Shin.Ellis
#  study.name group.name  n mpre mpos sdpre sdpos   r autoreg  t sdif F1 sdp df2 post control outcome ESL prof scope type
#1 Shin.Ellis   ME.short 13 0.34 0.72  0.37  0.34 0.5   FALSE NA   NA NA  NA  NA    1   FALSE       1   1    2     1    2
#2 Shin.Ellis    ME.long 13 0.34 0.39  0.37  0.36 0.5    TRUE NA   NA NA  NA  NA    2   FALSE       1   1    2     1    2
#3 Shin.Ellis  DCF.Short 15 0.37 0.54  0.38  0.36 0.5   FALSE NA   NA NA  NA  NA    1   FALSE       1   1    2     1    2
#4 Shin.Ellis   DCF.Long 15 0.37 0.49  0.38  0.36 0.5    TRUE NA   NA NA  NA  NA    2   FALSE       1   1    2     1    2
#5 Shin.Ellis Cont.Short 16 0.32 0.28  0.37  0.36 0.5   FALSE NA   NA NA  NA  NA    1    TRUE       1   1    2     1    2
#6 Shin.Ellis  Cont.Long 16 0.32 0.35  0.37  0.32 0.5    TRUE NA   NA NA  NA  NA    2    TRUE       1   1    2     1    2

#$Trus.Hsu
#  study.name group.name  n   mpre   mpos  sdpre  sdpos   r autoreg  t sdif F1 sdp df2 post control outcome ESL prof scope type
#8   Trus.Hsu      Exper 21 0.0799 0.1130 0.0367 0.0472 0.5   FALSE NA   NA NA  NA  NA    1   FALSE       1   2    2     2    1
#9   Trus.Hsu       Cont 26 0.0763 0.1095 0.0389 0.0537 0.5   FALSE NA   NA NA  NA  NA    1    TRUE       1   2    2     2    1

#$kabla
#   study.name group.name  n mpre mpos sdpre sdpos   r autoreg  t sdif F1 sdp df2 post control outcome ESL prof scope type
#11      kabla   ME.short 13 0.34 0.72  0.37  0.34 0.5   FALSE NA   NA NA  NA  NA    1   FALSE       1   1    3     0    1
#12      kabla    ME.long 13 0.34 0.39  0.37  0.36 0.5   FALSE NA   NA NA  NA  NA    2   FALSE       1   1    3     0    1
#13      kabla  DCF.Short 15 0.37 0.54  0.38  0.36 0.5   FALSE NA   NA NA  NA  NA    1   FALSE       1   1    3     0    1
#14      kabla   DCF.Long 15 0.37 0.49  0.38  0.36 0.5   FALSE NA   NA NA  NA  NA    2   FALSE       1   1    3     0    1
#15      kabla Cont.Short 16 0.32 0.28  0.37  0.36 0.5   FALSE NA   NA NA  NA  NA    1    TRUE       1   1    3     0    1
#16      kabla  Cont.Long 16 0.32 0.35  0.37  0.32 0.5   FALSE NA   NA NA  NA  NA    2    TRUE       1   1    3     0    1
Related