Change the alignment of the staircase in R

☆樱花仙子☆ 提交于 2020-01-17 05:53:07

问题


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.


回答1:


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")
}


来源:https://stackoverflow.com/questions/40962641/change-the-alignment-of-the-staircase-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!