Change the alignment of the staircase in R

2019-09-10 02:17发布

I am trying to make staircase of given length n using the following function:

hash<-function(n){
  for (i in 1:n){
    v1=c()
    #j=1
    for (j in 1:i){
      v1=paste("#",v1,sep="")
    }
    cat(v1,"\n")
  }
}

But I want it right aligned. What I am getting is:

 # 
 ## 
 ### 
 #### 
 ##### 
 ######

I was wondering, can I get some help how to make it aligned the other way?thanks for the assistance.

标签: r paste cat
1条回答
\"骚年 ilove
2楼-- · 2019-09-10 02:49
n=6
for (i in 1:n) {
  v1=c()
  v2=c()
  for (j in i:n-1) {
     v1=paste(" ",v1,sep="")
  }
  for (k in i:1) {
     v2=paste("#",v2,sep="")
  }
  cat(v1,v2,"\n")
}
查看更多
登录 后发表回答