0

This is my matrix in R:

n_forward=50

matriz=matrix(0,nrow=length(1:n_forward),ncol=16)

The number of rows depends of n_forward command.

from there I want to name de rows with this legends: t+1, t+2,....,t+(n_forward)

Is there any way to do this automatically, without having to do this below?

rownames(matriz)<-c("t+1","t+2","t+3","t+4","t+5","t+6","t+7","t+8","t+9","t+10","t+11","t+12","t+13","t+14","t+15","t+16","t+17","t+18","t+19","t+20","t+21","t+22","t+23","t+24","t+25","t+26","t+27","t+28","t+29","t+30", "t+31","t+32","t+33","t+34","t+35","t+36","t+37","t+38","t+39","t+40","t+41","t+42","t+43","t+44","t+45","t+46","t+47","t+48","t+49","t+50")
6
  • like this: n_forward=101 matriz=matrix(0,nrow=length(1:n_forward),ncol=16) rownames(matriz)<-paste0("t+", 1:length(n_forward)) ? Commented Nov 1, 2017 at 14:11
  • Thanks! It works now. I will edit the code. n_forward=1:101 matriz=matrix(0,nrow=length(n_forward),ncol=16) rownames(matriz)<-paste0("t+", 1:length(n_forward)) Commented Nov 1, 2017 at 14:15
  • 1
    If you run length(n_forward) it returns 1. Instead you need to omit the length function. So, rownames(matriz)<- paste0("t+", 1:n_forward) should work. Commented Nov 1, 2017 at 14:15
  • 1
    So which code worked, mine or that from @DiogoBastos ? Commented Nov 1, 2017 at 14:18
  • 4
    If n_forward could ever be 0, then seq_len(n_forward) is safer than 1:n_forward. The first would return an empty vector, the second would return c(1, 0). Commented Nov 1, 2017 at 14:19

1 Answer 1

1

What you need is this:

n_forward=50
matriz=matrix(0,nrow=length(1:n_forward),ncol=16)
rownames(matriz)<-paste0("t+",1:n_forward)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.