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]
13 个解决方案
#1
232
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"
#2
389
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:
这段代码大致如下:
-
names(df)
looks into all the names in thedf
- names(df)检查df中的所有名称
-
[names(df) == old.var.name]
extracts the variable name you want to check - [names(df) == old.var.name]提取要检查的变量名
-
<- 'new.var.name'
assigns the new variable name. - <- 'new.var.name'分配新变量名。
#3
61
colnames(df)[colnames(df) == 'oldName'] <- 'newName'
#4
56
This is an old question, but it is worth noting that you can now use setnames
from the data.table
package.
这是一个老问题,但值得注意的是,您现在可以从数据中使用setnames。表方案。
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)
#5
43
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
#6
17
I think the best way of renaming columns is by using the dplyr package like this:
我认为重命名列的最好方法是使用dplyr包,如下所示:
require(dplyr)
df = rename(df, new_col01 = old_col01, new_col02 = old_col02, ...)
It works the same for renaming one or many columns in any dataset.
它对在任何数据集中重命名一个或多个列都是一样的。
#7
8
I like the next style for rename dataframe column names one by one.
我喜欢重命名dataframe列名的下一个样式。
colnames(df)[which(colnames(df) == 'old_colname')] <- 'new_colname'
where
在哪里
which(colnames(df) == 'old_colname')
returns by the index of the specific column.
按特定列的索引返回。
#8
3
Try:
试一试:
colnames(x)[2] <- 'newname2'
#9
3
You can use the rename.vars
in the gdata
package.
您可以使用重命名。gdata包中的vars。
library(gdata)
df <- rename.vars(df, from = "oldname", to = "newname")
This is particularly useful where you have more than one variable name to change or you want to append or pre-pend some text to the variable names, then you can do something like:
当你有多个变量名需要更改,或者你想在变量名中添加或预先添加一些文本时,这是特别有用的,然后你可以做如下的事情:
df <- rename.vars(df, from = c("old1", "old2", "old3",
to = c("new1", "new2", "new3"))
For an example of appending text to a subset of variables names see: https://*.com/a/28870000/180892
有关将文本附加到变量名称子集的示例,请参见:https://*.com/a/28870000/180892
#10
2
If you know that your dataframe has only one column, you can use: names(trSamp) <- "newname2"
如果您知道您的dataframe只有一个列,您可以使用:names(trSamp) <- "newname2"
#11
2
You could also try 'upData' from 'Hmisc' package.
您也可以尝试“Hmisc”包中的“upData”。
library(Hmisc)
库(Hmisc)
trSamp = upData(trSamp, rename=c(sample.trainer.index..10000. = 'newname2'))
trSamp = upData(trSamp,重命名= c(sample.trainer.index . . 10000。= ' newname2 '))
#12
2
I find that the most convenient way to rename a single column is using dplyr::rename_at
:
我发现重命名单个列最方便的方法是使用dplyr::rename_at:
library(dplyr)
cars %>% rename_at("speed",~"new") %>% head
cars %>% rename_at(vars(speed),~"new") %>% head
cars %>% rename_at(1,~"new") %>% head
# new dist
# 1 4 2
# 2 4 10
# 3 7 4
# 4 7 22
# 5 8 16
# 6 9 10
- works well in pipe chaines
- 在管治中工作很好。
- convenient when names are stored in variables
- 当名称存储在变量中时非常方便
- works with a name or an column index
- 使用名称或列索引
- clear and compact
- 清晰而紧凑的
#13
1
This is likely already out there, but I was playing with renaming fields while searching out a solution and tried this on a whim. Worked for my purposes.
这很可能已经存在了,但我在寻找解决方案时尝试重新命名字段,并心血来潮地尝试了一下。为我工作的目的。
Table1$FieldNewName <- Table1$FieldOldName
Table1$FieldOldName <- NULL
Edit begins here....
在这里编辑开始....
This works as well.
这个作品。
df <- rename(df, c("oldColName" = "newColName"))
#1
232
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"
#2
389
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:
这段代码大致如下:
-
names(df)
looks into all the names in thedf
- names(df)检查df中的所有名称
-
[names(df) == old.var.name]
extracts the variable name you want to check - [names(df) == old.var.name]提取要检查的变量名
-
<- 'new.var.name'
assigns the new variable name. - <- 'new.var.name'分配新变量名。
#3
61
colnames(df)[colnames(df) == 'oldName'] <- 'newName'
#4
56
This is an old question, but it is worth noting that you can now use setnames
from the data.table
package.
这是一个老问题,但值得注意的是,您现在可以从数据中使用setnames。表方案。
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)
#5
43
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
#6
17
I think the best way of renaming columns is by using the dplyr package like this:
我认为重命名列的最好方法是使用dplyr包,如下所示:
require(dplyr)
df = rename(df, new_col01 = old_col01, new_col02 = old_col02, ...)
It works the same for renaming one or many columns in any dataset.
它对在任何数据集中重命名一个或多个列都是一样的。
#7
8
I like the next style for rename dataframe column names one by one.
我喜欢重命名dataframe列名的下一个样式。
colnames(df)[which(colnames(df) == 'old_colname')] <- 'new_colname'
where
在哪里
which(colnames(df) == 'old_colname')
returns by the index of the specific column.
按特定列的索引返回。
#8
3
Try:
试一试:
colnames(x)[2] <- 'newname2'
#9
3
You can use the rename.vars
in the gdata
package.
您可以使用重命名。gdata包中的vars。
library(gdata)
df <- rename.vars(df, from = "oldname", to = "newname")
This is particularly useful where you have more than one variable name to change or you want to append or pre-pend some text to the variable names, then you can do something like:
当你有多个变量名需要更改,或者你想在变量名中添加或预先添加一些文本时,这是特别有用的,然后你可以做如下的事情:
df <- rename.vars(df, from = c("old1", "old2", "old3",
to = c("new1", "new2", "new3"))
For an example of appending text to a subset of variables names see: https://*.com/a/28870000/180892
有关将文本附加到变量名称子集的示例,请参见:https://*.com/a/28870000/180892
#10
2
If you know that your dataframe has only one column, you can use: names(trSamp) <- "newname2"
如果您知道您的dataframe只有一个列,您可以使用:names(trSamp) <- "newname2"
#11
2
You could also try 'upData' from 'Hmisc' package.
您也可以尝试“Hmisc”包中的“upData”。
library(Hmisc)
库(Hmisc)
trSamp = upData(trSamp, rename=c(sample.trainer.index..10000. = 'newname2'))
trSamp = upData(trSamp,重命名= c(sample.trainer.index . . 10000。= ' newname2 '))
#12
2
I find that the most convenient way to rename a single column is using dplyr::rename_at
:
我发现重命名单个列最方便的方法是使用dplyr::rename_at:
library(dplyr)
cars %>% rename_at("speed",~"new") %>% head
cars %>% rename_at(vars(speed),~"new") %>% head
cars %>% rename_at(1,~"new") %>% head
# new dist
# 1 4 2
# 2 4 10
# 3 7 4
# 4 7 22
# 5 8 16
# 6 9 10
- works well in pipe chaines
- 在管治中工作很好。
- convenient when names are stored in variables
- 当名称存储在变量中时非常方便
- works with a name or an column index
- 使用名称或列索引
- clear and compact
- 清晰而紧凑的
#13
1
This is likely already out there, but I was playing with renaming fields while searching out a solution and tried this on a whim. Worked for my purposes.
这很可能已经存在了,但我在寻找解决方案时尝试重新命名字段,并心血来潮地尝试了一下。为我工作的目的。
Table1$FieldNewName <- Table1$FieldOldName
Table1$FieldOldName <- NULL
Edit begins here....
在这里编辑开始....
This works as well.
这个作品。
df <- rename(df, c("oldColName" = "newColName"))