0
我正在寫一個置換函數如何重新安排一個函數內的變量表?
permutation<-function(seed,treatment,block_number,block_sizes,sample.size){
b<-block_number
s<-block_sizes ## number of subjects per block
set.seed(seed)
m<-sample(s,size=b,replace = T) ## how the block is assigned
treats<-vector("list",b)
for (i in 1:b){
treats[[i]]<-sample(rep(treatment,each=m[i]))## permutation within each block
}
assign<-unlist(treats)[1:sample.size]
table<-data.frame(cbind(as.numeric(c(1:sample.size)),assign))
return(table)
}
permutation(seed=1,treatment=c('a',"b"),block_number=4,block_sizes=c(2,3),sample.size = 15)
我的輸出是一個數據幀是這樣的:
V1 assign
1 1 a
2 2 b
3 3 a
4 4 b
5 5 b
6 6 a
7 7 b
8 8 a
9 9 b
10 10 a
11 11 b
12 12 b
13 13 a
14 14 a
15 15 a
我要的是黃泥重新排列輸出表,在這樣的格式(1,2,3中的圖片應該是A,b以我爲例):
我試着取出表,可以使用subset()和cbind()強制組合這兩列,但我不知道如何在函數內寫入它。