我想对415名受试者进行探索性因素分析和验证性因素分析。我想把我的样本分成两个大致相等的样本,这样我就可以对208名受试者进行全民教育,对207名受试者进行终审。
我使用sample()函数:
a<-sample(1:415,208,replace=F)
sort(a)
b<-sample(1:415,207,replace=F)
sort(b)我得到了不同的值。
我想为"a“中提到的所有208个参与者在我的数据集的"AFE.AFC”列中分配一个数字1,并在同一列中为"b“中提到的207个参与者分配一个数字2。
你知道什么公式可以做到这一点吗?
谢谢你的帮助!
发布于 2017-05-13 06:07:39
所以你实际上并不想使用sample两次,因为你很可能会得到一些数字,这些数字在两个地方都会出现。假设您的数据在一个名为df的数据帧中,您可以这样做
df$AFE.AFC <- 2 # this creates a column called AFE.AFC where all the values are 2
a.index <- sample(1:415, 208, replace = FALSE) # randomly samples 208 numbers from 1-415
df$AFE.AFC[a.index] <- 1 # this changes the value in the column category and changes it to 1 if it's the row number it's in is in AFE.AFC诀窍在于第三行,也就是最后一行。如果数字5, 21, 103, 4, ...在a.index中,那么df$AFE.AFC的第5、第21、第103和第4个值将从1更改为2。
https://stackoverflow.com/questions/43944167
复制相似问题