首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改R中的行名

更改R中的行名
EN

Stack Overflow用户
提问于 2022-09-06 14:59:28
回答 2查看 43关注 0票数 0

我有两个具有类似行名的数据文件:

代码语言:javascript
复制
> rownames(abundance)[1:10]
 [1] "X001.V2.fastq_mapped_to_agora.txt.uniq" 
 [2] "X001.V8.fastq_mapped_to_agora.txt.uniq" 
 [3] "X003.V17.fastq_mapped_to_agora.txt.uniq"
 [4] "X003.V2.fastq_mapped_to_agora.txt.uniq" 
 [5] "X003.V8.fastq_mapped_to_agora.txt.uniq" 
 [6] "X004.V2.fastq_mapped_to_agora.txt.uniq" 
 [7] "X004.V8.fastq_mapped_to_agora.txt.uniq" 
 [8] "X005.V2.fastq_mapped_to_agora.txt.uniq" 
 [9] "X005.V8.fastq_mapped_to_agora.txt.uniq" 
[10] "X006.V2.fastq_mapped_to_agora.txt.uniq" 
> rownames(fluxes)[1:10]
 [1] "001.V8"  "003.V17" "003.V2"  "003.V8"  "004.V2"  "004.V8"  "005.V2" 
 [8] "005.V8"  "006.V2"  "006.V8" 

但是数据丰度的行名更大。如何使每一行的名称像的行名一样。可以是从"X“到”第二“。”

EN

回答 2

Stack Overflow用户

发布于 2022-09-06 15:02:53

我们可以使用trimws

代码语言:javascript
复制
rownames(abundance) <- trimws(rownames(abundance), whitespace = "\\..*")

也可能是

代码语言:javascript
复制
rownames(abundance) <- sub("^([^.]+\\.[^.]+)\\..*", "\\1", rownames(abundance))

-testing

代码语言:javascript
复制
> trimws("X001.V2.fastq_mapped_to_agora.txt.uniq",  whitespace = "\\..*")
[1] "X001"
> sub("^([^.]+\\.[^.]+)\\..*", "\\1", "X001.V2.fastq_mapped_to_agora.txt.uniq")
[1] "X001.V2"
票数 2
EN

Stack Overflow用户

发布于 2022-09-06 15:05:04

我们可以用sub

代码语言:javascript
复制
rownames(abundance) <- sub("X(.*)\\.fastq_mapped_to_agora\\.txt\\.uniq", "\\1", rownames(abundance))

输出:

代码语言:javascript
复制
[1] "001.V2"  "001.V8"  "003.V17" "003.V2"  "003.V8"  "004.V2"  "004.V8"  "005.V2"  "005.V8"  "006.V2" 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73624121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档