如何重命名data.frame中的單個列?

本文翻譯自: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. 通過特定列的索引返回。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章