如何在R dataframe中不加修改地使name成为行名称

c9qzyr3d  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(115)

我在R中将其中一个列名作为数据框中的行名称。然而,每当我做同样的事情时,X在数字之前加起来,连字符(-)被点(.)代替。有没有办法在R中处理这个问题,保持名称不变。这些类型的更改在合并两个数据框时会产生问题。请帮助我。
下面是代码和dataframe的示例:

之前

dput(tail(Probe.annotation.table, 10))
structure(list(ID = c("100306034_TGI_at", "100311622_TGI_at", 
"merck2-rsta-239398_at", "merck2-NM_000839-381.A_at", "merck2-NM_004103-360.B_at", 
"merck2-HCV_at", "merck2-HCV_revcomp_at", "merck2-HIV_at", "merck2-HIV_revcomp_at", 
"merck2-flu_at"), EntrezGeneID = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), GeneSymbol = c("", "", "", "", "", "", "", "", "", ""), GB_ACC = c("", 
"", "", "", "", "", "", "", "", ""), miRNA_ID = c("", "", "", 
"", "", "", "", "", "", ""), ORF = c("", "", "", "", "", "", 
"", "", "", ""), PROBE_DERIVED_FROM_TRANSCRIPT = c("", "", "", 
"", "", "", "", "", "", ""), SPOT_ID = c("RosettaGeneID:BX647964", 
"RosettaGeneID:AK124699", "RosettaGeneID:rsta-239398", "RosettaGeneID:NM_000839-381.A", 
"RosettaGeneID:NM_004103-360.B", "RosettaGeneID:HCV", "RosettaGeneID:HCV_revcomp", 
"RosettaGeneID:HIV", "RosettaGeneID:HIV_revcomp", "RosettaGeneID:flu"
), RosettaGeneModelID = c("", "", "", "", "", "", "", "", "", 
"")), row.names = 52369:52378, class = "data.frame")

代码

rownames(Probe.annotation.table) <- make.names(Probe.annotation.table$ID, unique=TRUE)

之后

put(tail(Probe.annotation.table, 10))
structure(list(ID = c("100306034_TGI_at", "100311622_TGI_at", 
"merck2-rsta-239398_at", "merck2-NM_000839-381.A_at", "merck2-NM_004103-360.B_at", 
"merck2-HCV_at", "merck2-HCV_revcomp_at", "merck2-HIV_at", "merck2-HIV_revcomp_at", 
"merck2-flu_at"), EntrezGeneID = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), GeneSymbol = c("", "", "", "", "", "", "", "", "", ""), GB_ACC = c("", 
"", "", "", "", "", "", "", "", ""), miRNA_ID = c("", "", "", 
"", "", "", "", "", "", ""), ORF = c("", "", "", "", "", "", 
"", "", "", ""), PROBE_DERIVED_FROM_TRANSCRIPT = c("", "", "", 
"", "", "", "", "", "", ""), SPOT_ID = c("RosettaGeneID:BX647964", 
"RosettaGeneID:AK124699", "RosettaGeneID:rsta-239398", "RosettaGeneID:NM_000839-381.A", 
"RosettaGeneID:NM_004103-360.B", "RosettaGeneID:HCV", "RosettaGeneID:HCV_revcomp", 
"RosettaGeneID:HIV", "RosettaGeneID:HIV_revcomp", "RosettaGeneID:flu"
), RosettaGeneModelID = c("", "", "", "", "", "", "", "", "", 
"")), row.names = c("X100306034_TGI_at", "X100311622_TGI_at", 
"merck2.rsta.239398_at", "merck2.NM_000839.381.A_at", "merck2.NM_004103.360.B_at", 
"merck2.HCV_at", "merck2.HCV_revcomp_at", "merck2.HIV_at", "merck2.HIV_revcomp_at", 
"merck2.flu_at"), class = "data.frame")
wljmcqd8

wljmcqd81#

试试这个:

library(dplyr)
newdata <- Probe.annotation.table %>%
  as_tibble() %>%
  column_to_rownames(var = "ID")

相关问题