如何在不修改的情况下使名称成为R数据框中的行名称
How to make name a row name in R dataframe without modifications
我正在将其中一个列名设为 R 数据框中的行名。但是,每当我做同样的事情时,X 在数字之前相加,连字符 (-) 被点 ( .) 有没有办法在 R 中处理这个问题,以保持名称不变。这些类型的更改在合并两个数据帧时会产生问题。请协助我。
这是代码和数据框的示例:
之前
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")
试试这个:
library(dplyr)
newdata <- Probe.annotation.table %>%
as_tibble() %>%
column_to_rownames(var = "ID")
我正在将其中一个列名设为 R 数据框中的行名。但是,每当我做同样的事情时,X 在数字之前相加,连字符 (-) 被点 ( .) 有没有办法在 R 中处理这个问题,以保持名称不变。这些类型的更改在合并两个数据帧时会产生问题。请协助我。
这是代码和数据框的示例:
之前
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")
试试这个:
library(dplyr)
newdata <- Probe.annotation.table %>%
as_tibble() %>%
column_to_rownames(var = "ID")