简体   繁体   中英

Adding a character element to a numerical matrix

I have a 2 x 30 matrix in R consisting of 1s and 2s. I was wondering how I could add an additional "p:" before each element of my matrix?

For example, p1: 1, p2: 2, p3: 1, p4: 2 and so on.

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=''))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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