如何重命名 data.frame 中的单个列?

编程入门 行业动态 更新时间:2024-10-27 04:25:03
本文介绍了如何重命名 data.frame 中的单个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道如果我有一个多于 1 列的数据框,我可以使用

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.

示例:

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]

推荐答案

colnames(trSamp)[2] <- "newname2"

尝试设置第二列的名称.您的对象只有一列,因此该命令会引发错误.这应该足够了:

attempts to set the second column's name. Your object only has one column, so the command throws an error. This should be sufficient:

colnames(trSamp) <- "newname2"

更多推荐

如何重命名 data.frame 中的单个列?

本文发布于:2023-10-20 03:21:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   data   frame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!