在下面的R代码中,我按列split
和data.frame
,称为study.name
的字符串变量。
但是split
按字母顺序重新排序原始的data.frame
。在 BASE R 中,是否可以在拆分后保留原始数据顺序?
D <- read.csv("https://raw.githubusercontent.com/izeh/i/master/k.csv", h = T) # data.frame
m <- split(D, D$study.name)
答案 0 :(得分:2)
我们可以split
通过factor
转换后的'study.name',其中levels
被指定为列的unique
元素,而unique
返回值以唯一元素出现的相同顺序
split(D, factor(D$study.name, levels = unique(D$study.name)))
如果我们需要删除NA
元素,请在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