在R中split()之后保持数据的原始顺序 [英] keeping the original order of data after split() in R
问题描述
在下面的R代码中,我按列split
和data.frame
,是一个名为study.name
的字符串变量.
In the following R code, I split
a data.frame
by a column, a string variable called study.name
.
但是split
按字母顺序按顺序重新排列原始data.frame
.在 BASE R 中,是否可以在拆分后保留原始数据顺序?
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)
推荐答案
我们可以通过factor
转换后的'study.name'来split
,其中levels
被指定为列的unique
元素,并且unique
以出现唯一元素的相同顺序返回值
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)))
如果我们需要删除NA
元素,请在split
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
这篇关于在R中split()之后保持数据的原始顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!