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

本文介绍了在R语言中如何重命名只有一个列的数据框。提供了多种方法,包括直接操作、使用Hmisc包的upData函数、利用dplyr的rename函数,以及tidyverse包的rename_at方法。这些技巧允许用户根据列名或列的位置进行重命名,适用于不同的场景需求。
摘要由CSDN通过智能技术生成

本文翻译自:How to rename a single column in a data.frame?

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]

#1楼

参考:https://stackoom.com/question/VbNk/如何重命名data-frame中的单个列


#2楼

您也可以尝试从“ Hmisc”包中获取“ upData”。

library(Hmisc)

trSamp = upData(trSamp, rename=c(sample.trainer.index..10000. = 'newname2'))


#3楼

This is a generalized way in which you do not have to remember the exact location of the variable: 这是一种通用的方式,您不必记住变量的确切位置:

# df = dataframe
# old.var.name = The name you don't like anymore
# new.var.name = The name you want to get

names(df)[names(df) == 'old.var.name'] <- 'new.var.name'

This code pretty much does the following: 此代码几乎可以执行以下操作:

  1. names(df) looks into all the names in the df names(df)的外观到在所有的名字df
  2. [names(df) == old.var.name] extracts the variable name you want to check [names(df) == old.var.name]提取您要检查的变量名
  3. <- 'new.var.name' assigns the new variable name. <- 'new.var.name'分配新的变量名称。

#4楼

This can also be done using Hadley's plyr package, and the rename function. 这也可以使用Hadley的plyr包和rename功能来完成。

library(plyr) 
df <- data.frame(foo=rnorm(1000)) 
df <- rename(df,c('foo'='samples'))

You can rename by the name (without knowing the position) and perform multiple renames at once. 您可以按名称重命名(不知道位置),并一次执行多个重命名。 After doing a merge, for example, you might end up with: 例如,进行合并后,您可能会得到以下结果:

  letterid id.x id.y
1       70    2    1
2      116    6    5
3      116    6    4
4      116    6    3
5      766   14    9
6      766   14   13

Which you can then rename in one step using: 然后您可以使用以下步骤一步重命名:

letters <- rename(letters,c("id.x" = "source", "id.y" = "target"))

  letterid source target
1       70      2      1
2      116      6      5
3      116      6      4
4      116      6      3
5      766     14      9
6      766     14     13

#5楼

This is an old question, but it is worth noting that you can now use setnames from the data.table package. 这是一个老问题,但值得注意的是,您现在可以使用setnamesdata.table包。

library(data.table)

setnames(DF, "oldName", "newName")

# or since the data.frame in question is just one column: 
setnames(DF, "newName")

# And for reference's sake, in general (more than once column)
nms <- c("col1.name", "col2.name", etc...)
setnames(DF, nms)

#6楼

I like the next style for rename dataframe column names one by one. 我喜欢一种用于逐一重命名数据框列名称的样式。

colnames(df)[which(colnames(df) == 'old_colname')] <- 'new_colname'

where 哪里

which(colnames(df) == 'old_colname')

returns by the index of the specific column. 通过特定列的索引返回。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值