如何将输出列表打印为R中的一行

如何将输出列表打印为R中的一行,r,function,R,Function,作为an练习的一部分,我应该编写一个函数来替换R中的seq()命令 我成功地制作了一个类似的: seq2 <- function(a,r,n){ #a is the starting number, r is the increment, n is the length out i <- a k <- 1 repeat{ print(i) i<-i+r k=k+1 if(k>n) break } } 输出是 [1]

作为an练习的一部分,我应该编写一个函数来替换R中的
seq()
命令

我成功地制作了一个类似的:

seq2 <- function(a,r,n){  #a is the starting number, r is the increment, n is the length out
 i <- a
 k <- 1
 repeat{
   print(i)
   i<-i+r
   k=k+1
   if(k>n)
     break
  }
}
输出是

 [1] 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80
而我的代码有以下输出:

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
那么,有没有办法调整我的代码,使其产生与
seq()
命令相同的输出


谢谢

您可以在函数中创建一个新向量,并在末尾返回该向量:

seq2 <- function(a,r,n){  #a is the starting number, r is the increment, n is the length out
  i <- a
  k <- 1
  out = c()
  repeat{
    out = c(out,i)
    i<-i+r
    k=k+1
    if(k>n)
      break
 }
 return(out)
}

seq2谢谢!工作完美:D
seq2 <- function(a,r,n){  #a is the starting number, r is the increment, n is the length out
  i <- a
  k <- 1
  out = c()
  repeat{
    out = c(out,i)
    i<-i+r
    k=k+1
    if(k>n)
      break
 }
 return(out)
}