1

我的问题很简单。我想命名时间序列(两个时间序列)数据的行,以便每个行名重复两次,但用“-1”和“-2”分隔它们,使其看起来像这样:

1-1  7.116864
1-2  6.898450
2-1  7.224002
2-2  6.993221
3-1  7.266787
3-2  7.483816
4-1  8.055825
4-2  7.993788
5-1 8.895424
5-2 9.097769
7-1  7.116864
7-2  6.898450
10-1  7.224002
10-2  6.993221

我知道我可以很容易地使用 do.NULLdimnames(matrixm) <- list(rownames(matrixm, do.NULL = FALSE, prefix = "row"))为矩阵矩阵命名行,但这里我的第一个问题是

对于从 t 1-1、1-2 到 t 5-1 和 5-2 的两个时间序列,我如何按上述顺序命名?

其次,我是否也可以选择其他任意行并以相同的方式命名它们,例如第 7 和第 10 个时间序列?

4

3 回答 3

1

这是我的实现:

# length of your time series vector
n <- 20
# simulate the values of the series vector
x <- matrix(rnorm(n,5,0.5),nr=n,nc=1)
# assign the rownames to the matrix
rownames(x) <- c(t(cbind(paste(1:(n/2),"1",sep="-"),
                         paste(1:(n/2),"2",sep="-"))))

# > x
#          [,1]
# 1-1  5.582177
# 1-2  5.320359
# 2-1  4.958729
# 2-2  4.630183
# 3-1  5.357314
# 3-2  4.287579
# 4-1  4.664101
# 4-2  5.299628
# 5-1  5.480967
# 5-2  5.368158
# 6-1  4.697664
# 6-2  5.191149
# 7-1  4.418201
# 7-2  4.399364
# 8-1  4.845039
# 8-2  4.785453
# 9-1  5.933396
# 9-2  4.035960
# 10-1 4.539947
# 10-2 4.162826
于 2013-09-15T20:48:42.027 回答
1

只需使用 rep 和 paste 命令的组合,如下所示:

# simulate your data
m <- matrix(runif(20,6,10))
# give names to rows
rownames(m) <- paste(rep(1:(nrow(m)/2), each=2), rep(1:2, nrow(m)/2), sep="-")
于 2013-09-15T20:49:41.763 回答
1

由于您没有给出可重复的示例,因此不清楚您想做什么。我认为您可以使用paste创建行名,然后使用rbindand合并它们order。例如:

set.seed(1)
t1 <- as.matrix(rnorm(5))
t2 <- as.matrix(rnorm(5))
rownames(t1) <- paste(1:5,1,sep='-')
rownames(t2) <- paste(1:5,2,sep='-')

然后使用合并矩阵rbind并对它们进行排序:

tt <- rbind(t1,t2)
tt[order(rownames(tt)),,drop=FALSE]

          [,1]
1-1 -0.6264538
1-2 -0.8204684
2-1  0.1836433
2-2  0.4874291
3-1 -0.8356286
3-2  0.7383247
4-1  1.5952808
4-2  0.5757814
5-1  0.3295078
5-2 -0.3053884
于 2013-09-15T20:38:08.203 回答