我有一个表格
的data.table nrow V1 V2 V3 V4 V5 V6 V7 V8
1: 1 chr20 C4b 6382078 C4b 6381944 reg - Thymus_M_GSM1328751
2: 2 chr8 Rpl4 68832532 Rpl4 68832743 reg + Thymus_M_GSM1328751
我想将列命名为(“Chromosome”,“Start_Gene”,“startPosition”,“End_gene”,“endPosition”,“RNAtype”,“strand”,“replicate”)。我试过了:
ratData <- ratData[, by= list("Chromosome", "Start_Gene","startPosition", "End_gene", "endPosition", "RNAtype", "strand", "replicate")]
但它不起作用。我也尝试过键而不是“by”。
我该怎么做?
答案 0 :(得分:3)
您可以使用setnames
功能更改列名称。
setnames(ratData, c("nrow", "Chromosome", "Start_Gene","startPosition", "End_gene",
"endPosition", "RNAtype", "strand", "replicate"))
以下是一些变体:
setnames(ratData,
paste0("V", 1:8), # i.e. c("V1", "V2", ..., "V8")
c("Chromosome", "Start_Gene","startPosition", "End_gene",
"endPosition", "RNAtype", "strand", "replicate"))
setnames(ratData,
2:9,
c("Chromosome", "Start_Gene","startPosition", "End_gene",
"endPosition", "RNAtype", "strand", "replicate"))
答案 1 :(得分:-2)
'names'功能也有效:
names(ratData)
[1] "nrow" "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8"
names(ratData) = c("nrow", "Chromosome", "Start_Gene","startPosition", "End_gene", "endPosition", "RNAtype", "strand", "replicate")
ratData
nrow Chromosome Start_Gene startPosition End_gene endPosition RNAtype strand replicate
1: 1 chr20 C4b 6382078 C4b 6381944 reg 0 Thymus_M_GSM1328751
2: 2 chr8 Rpl4 68832532 Rpl4 68832743 reg 0 Thymus_M_GSM1328751