2017-07-26 72 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