如何在 R 中的特定数字和字符之间添加连字符

问题描述 投票:0回答:0
This is my data set and I want to put hypens between specific digits and charecters.

df_t
                  1 2 3 4 5 6         row_names
X2012SGA007930001 1 2 3 4 5 6 X2012SGA007930001
X2012SGA008360001 1 2 3 3 2 6 X2012SGA008360001
X2012SGA009170001 1 2 3 4 5 6 X2012SGA009170001
X2012SGA009180001 1 3 2 3 4 2 X2012SGA009180001
X2012SGA009200001 3 4 5 4 2 1 X2012SGA009200001
X2012SGA009210001 4 5 6 7 8 9 X2012SGA009210001

图书馆(整理) 库(stringr)

y<- c(str_remove(df_t$row_names, pattern = "X")) y is.vector(y) x<- c(substr( y, start = 1,stop=5)) x is.vector(x) z<- c(substr(y, start=6, stop=7)) z

t<- c(substr(y, start=8, stop=12)) t

u<- c(substr(y, start=13, stop=16)) u If I look to x,z,t,u I can see that I seperated it according I want it.

数据<- paste(df_t, col='Genetic', c('x','z','t','u'), sep="-") data

我的问题是将 row_names 变成如下:

                          1     2      3    4       5      6         Genetic

                          1     2      3    4       5     "2012S-GA-00793-0001"
                          2     3      4    5       6     "2012S-GA-00836-0001"
                          3     4      4    5       2     "2012S-GA-00917-0001" 
                          2     1      2    3       5     "2012S-GA-00920-0001"                
                          3     3      5    4       7     "2012S-GA-00921-0001" 

但是当我想将这些 x,z,t,u 放在一起并将行名称更改为遗传时,它不起作用。我尝试使用联合功能,但它也不起作用。 有谁知道如何在特定数字和字符之间添加连字符。提前致谢。

r paste gsub substr digits
© www.soinside.com 2019 - 2024. All rights reserved.