我知道如果我有一个多于一列的数据框,我可以使用
colnames(x) <- c("col1","col2")
来重命名列。 如果只有一栏我该怎么做? 表示其中仅包含一列的向量或数据帧。
例如:
trSamp <- data.frame(sample(trainer$index, 10000))
head(trSamp )
# sample.trainer.index..10000.
# 1 5907862
# 2 2181266
# 3 7368504
# 4 1949790
# 5 3475174
# 6 6062879
ncol(trSamp)
# [1] 1
class(trSamp)
# [1] "data.frame"
class(trSamp[1])
# [1] "data.frame"
class(trSamp[,1])
# [1] "numeric"
colnames(trSamp)[2] <- "newname2"
# Error in names(x) <- value :
# 'names' attribute [2] must be the same length as the vector [1]
转载于:https://stackoverflow.com/questions/7531868/how-to-rename-a-single-column-in-a-data-frame