-1
考慮下面的代碼和它的回報:如何將函數的返回值轉換爲R中的數據框?
my_func<- function(t=4,n=100,m=0.009){
AA<- AB<- BB<- numeric(length = t+1)
AA[1]<- rbinom(1,n,1-m)
AB[1]<- BB[1]<- 0
for(step in 1:t){
AB[step+1]<- rbinom(1,AA[step],1-m)
BB[step+1]<- rbinom(1,AB[step],1-m)
AA[step+1]<- rbinom(1,BB[step],1-m)
}
out<- cbind(AA,AB,BB)
return(out)
}
my_func()
AA AB BB
[1,] 99 0 0
[2,] 0 99 0
[3,] 0 0 99
[4,] 98 0 0
[5,] 0 96 0
我希望能夠回報轉換成數據幀,這樣我可以使用在其他功能生成的輸出的部分。如何將其轉換爲數據框?
在此先感謝。
在'my_func'變化'出<的倒數第二行 - cbind(AA,AB,BB)''到出< - cbind.data.frame(AA, AB,BB)' – bouncyball
因爲您已經將矢量初始化爲零,所以不需要AB'[1] < - BB [1] < - 0'這一行。其次,不是兩行'out < - cbind(。)'和'return(。)',而是'data.frame(AA,AB,BB)'。 –
非常感謝您的幫助,@bouncyball –