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

    技术2022-07-11  119

    本文翻译自: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: 此代码几乎可以执行以下操作:

    names(df) looks into all the names in the df 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'分配新的变量名称。

    #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. 这是一个老问题,但值得注意的是,您现在可以使用setnames从data.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. 通过特定列的索引返回。

    Processed: 0.011, SQL: 9