How can I create a vector by only using for loop? (vector is specified in the body)
问题 (1,2,2,3,3,3,4,4,4,4,...,n,...,n) I want to make the above vector by for loop, but not using rep function or the other functions. This may not be good question to ask in stackoverflow, but since I am a newbie for R, I dare ask here to be helped. (You can suppose the length of the vector is 10) 回答1: With a for loop, it can be done with n <- 10 out <- c() for(i in seq_len(n)){ for(j in seq_len(i)) { out <- c(out, i) } } In R , otherwise, this can be done as rep(seq_len(n), seq_len(n)) 回答2: I