我有一个很大的列表,其中包含了许多细胞系表达的基因。Ensembl基因通常带有版本后缀,但我需要删除它们。我已经找到了几个描述这个这里或这里的引用,但是它们对我不起作用,可能是因为我的数据结构(我认为它是一个列表中的嵌套数组?)。有人能帮助我了解代码的细节和我对自己数据结构的理解吗?
以下是一些示例数据
>listOfGenes_version <- list("cellLine1" = c("ENSG001.1", "ENSG002.1", "ENSG003.1"), "cellLine2" = c("ENSG003.1", "ENSG004.1"))
>listOfGenes_version
$cellLine1
[1] "ENSG001.1" "ENSG002.1" "ENSG003.1"
$cellLine2
[1] "ENSG003.1" "ENSG004.1"我想看到的是
>listOfGenes_trimmed
$cellLine1
[1] "ENSG001" "ENSG002" "ENSG003"
$cellLine2
[1] "ENSG003" "ENSG004"以下是一些我试过但没有成功的事情
>listOfGenes_trimmed <- str_replace(listOfGenes_version, pattern = ".[0-9]+$", replacement = "")
Warning message:
In stri_replace_first_regex(string, pattern, fix_replacement(replacement), :
argument is not an atomic vector; coercing
>listOfGenes_trimmed <- lapply(listOfGenes_version, gsub('\\..*', '', listOfGenes_version))
Error in match.fun(FUN) :
'gsub("\\..*", "", listOfGenes_version)' is not a function, character or symbol非常感谢!
发布于 2019-09-03 20:28:13
一个选项是将模式指定为. (元字符-所以转义),在字符串末尾($)用一个或多个数字(\\d+)替换为'" ('")。
lapply(listOfGenes_version, sub, pattern = "\\.\\d+$", replacement = "")
#$cellLine1
#[1] "ENSG001" "ENSG002" "ENSG003"
#$cellLine2
#[1] "ENSG003" "ENSG004".是一个与任何字符匹配的元字符,因此我们需要转义它以获得文字值,因为默认情况下模式是regex。
https://stackoverflow.com/questions/57778327
复制相似问题