1
我有一個名爲dataOrder的數據框,其中列對應於樣本名稱(n = 384),行對應於基因實體(n = 180200)。R:替換數據框中的多個列名並保留其數值
sample1 sample2 sample3 sample4 sample5 sample6
ENST00000000233 9 0 3499.51 0 0 0
ENST00000000412 0 0 0.00 0 0 0
ENST00000000442 0 0 0.00 0 0 0
ENST00000001008 0 0 0.00 0 0 0
ENST00000001146 0 0 0.00 0 0 0
ENST00000002125 0 0 0.00 0 0 0
我想代替列名(STR sample
)的一部分,以五種不同的名稱:t1_,t2_,t3_,t4_和t5_。
我試圖用GSUB功能替代名稱:
nameVec <- names(dataOrder)
nameVec <- gsub("sample","t2_",nameVec[1:96])
nameVec <- gsub("sample","t3_",nameVec[97:163])
nameVec <- gsub("sample","t4_",nameVec[164:259])
nameVec <- gsub("sample","t5_",nameVec[260:333])
nameVec <- gsub("sample","t1_",nameVec[334:384])
names(dataOrder) <- nameVec
head(dataOrder)
然而,我所有的列名被取代,以NA。
如何在標題中替換「樣本」字符串並將數字索引保留在列中?
t1_1 t1_96 t2_97 t2_163 t3_164 t3_259
ENST00000000233 9 0 3499.51 0 0 0
ENST00000000412 0 0 0.00 0 0 0
ENST00000000442 0 0 0.00 0 0 0
ENST00000001008 0 0 0.00 0 0 0
ENST00000001146 0 0 0.00 0 0 0
ENST00000002125 0 0 0.00 0 0 0
這裏是(通過@RuiBarradas寫的)重複性的數據例如:
mydf <-
structure(list(target_id = c("ENST00000000233", "ENST00000000412",
"ENST00000000442", "ENST00000001008", "ENST00000001146", "ENST00000002125"
), sample1 = c(9L, 0L, 0L, 0L, 0L, 0L), sample10 = c(0L, 0L,
0L, 0L, 0L, 0L), sample100 = c(3499.51, 0, 0, 0, 0, 0), sample101 = c(0L,
0L, 0L, 0L, 0L, 0L), sample102 = c(0L, 0L, 0L, 0L, 0L, 0L), sample103 = c(0L,
0L, 0L, 0L, 0L, 0L)), .Names = c("target_id", "sample1", "sample10",
"sample100", "sample101", "sample102", "sample103"), class = "data.frame", row.names = c("1:",
"2:", "3:", "4:", "5:", "6:"))
result <- mydf[-1]
row.names(result) <- mydf$target_id
result
謝謝!
非常感謝! –