2017-07-26 40 views
0

我有一个110行的数据框,它是来自微阵列实验表达式集对象的pData。我想创建一个2级因子向量,随机分配给行(代表实验样本)。例如,如果在实验中有110行对应于110个主题,我希望将55行设置为“G0”,将55设置为“G1”。这些组用于后续功能。 我目前想这是一个函数内包裹下面我想修改:从随机标记数据帧的行中创建因子向量

# makes a numeric vector of the number of subjects/rows in the pData 
sml<-rep(0,length(colnames(eset)) 

# ‘populate’ sml with G0 & G1 
sml[sample(sml,(length(sml)/2))]<-"G0" 
sml[sample(sml,(length(sml)/2))]<-"G1" 
label <- as.factor(sml) 

我怎么样使得G1组完成的SML长度和叶已经被指定为G0不变的立场? 感谢

回答

2

这是正确的答案

eset <- matrix(NA, ncol = 110, nrow = 1) 
good <- sample(
    rep(
    factor(c("G0", "G1")), 
    ncol(eset) %/% 2 
) 
) 
table(good) 

这是坏榜样

bad <- sample(c("G0", "G1"), ncol(eset), replace = TRUE) 
table(bad) 
+0

真棒,这一次我不知道! – ricoderks

相关问题