Array Column Names using R

Asked

Viewed 1,513 times

2

This is my matrix in R:

n_forward=50

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

The number of lines depends on the command n_forward.

From there I want to name the lines in t+1, t+2,.... ,t+(n_forward)

There is how to do this automatically, without having to do it 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")

1 answer

4


Use paste together with seq_len.

rn <- paste("t", seq_len(n_forward), sep = "+")
rownames(matriz) <- rn

seq_len creates the sequence 1, 2, ..., n_forward. And paste does the rest.
Note that it is not necessary to create the vector rn, just do rownames(matriz) <- paste(...). I prefer the two lines for the code of a reply to be more readable, nothing more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.