I know if I have a data frame with more than 1 column, I can use
(我知道如果我有一个多于一列的数据框,我可以使用)
colnames(x) <- c("col1","col2")
to rename the columns.
(重命名列。)
How do I do this if it's just one column? (如果只有一栏,该怎么办?)
Meaning a vector or data frame with only one column in it. (表示其中仅包含一列的向量或数据帧。)
Example:
(例:)
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]
ask by screechOwl translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…