function - How to print a list of output as 1 line in R -
as part of exercise, supposed write function replaces seq() command in r.
i have managed make 1 works similarly:
seq2 <- function(a,r,n){ #a starting number, r increment, n length out <- k <- 1 repeat{ print(i) i<-i+r k=k+1 if(k>n) break } } however, output not want. example, when calling seq() command this:
seq(10,by=5,lenght.out=15) the output is
[1] 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 whereas code has output:
seq2(10,5,15) [1] 10 [1] 15 [1] 20 [1] 25 [1] 30 [1] 35 [1] 40 [1] 45 [1] 50 [1] 55 [1] 60 [1] 65 [1] 70 [1] 75 [1] 80 so there way adjust code produces same output seq() command?
thanks
you can create new vector within function , return vector @ end:
seq2 <- function(a,r,n){ #a starting number, r increment, n length out <- k <- 1 out = c() repeat{ out = c(out,i) i<-i+r k=k+1 if(k>n) break } return(out) }
Comments
Post a Comment