简体   繁体   English

将字符元素添加到数值矩阵

[英]Adding a character element to a numerical matrix

I have a 2 x 30 matrix in R consisting of 1s and 2s.我在R有一个 2 x 30 矩阵,由 1s 和 2s 组成。 I was wondering how I could add an additional "p:" before each element of my matrix?我想知道如何在矩阵的每个元素之前添加一个额外的"p:"

For example, p1: 1, p2: 2, p3: 1, p4: 2 and so on.例如p1: 1, p2: 2, p3: 1, p4: 2等等。

Here is what I tried with no success:这是我尝试但没有成功的方法:

a <- matrix(rep(1:2, 30), 2, 30)
a <- paste0("p", 1:30, ":")

我们paste “P”与序列,然后paste ,与matrix “a”和分配输出回到“A”

a[] <- paste(paste0("p", 1:30), a, sep=": ")

使用 apply 的另一种方法:

apply(a, 2, function(x) paste('p', parent.frame()$i[], ': ', x, sep=''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM