folk, I am quite new to coding and R. I am practicing writing a permutation function in R, and this this function was supposed to return all possibilities of rearrangement elements in a given vector. Here is my code:
"
result<-data.frame()
counts<-0
swap<-function(arrx,a,b){p1=arrx[a];p2=arrx[b];arrx[a]=p2;arrx[b]=p1;return(arrx)}
permu<-function(arrx,k){
m=length(arrx)
if (k==m)
{cat(arrx,"\n");counts=counts+1;result<-rbind(result,arrx)}
else {for (i in k:m){
arrx<- swap(arrx,i,k)
permu(arrx,k+1)
arrx<- swap(arrx,i,k)
}}
return(list(result,counts))}
permu(c("a","b","c"),1)
" after running it in R studio, I got this: "
> permu(c("a","b","c"),1)
a b c
a c b
b a c
b c a
c b a
c a b
[[1]]
data frame with 0 columns and 0 rows
[[2]]
[1] 0
" it seems that it can do the recursive permute job correctly. However, it cannot save the result in a dataframe via "return()". I googled a bit and found that in python there is a function called "yield", but in R, I cannot figure this out. How should I save the results in the way I want in R?