使用R在数据框中重命名列

问题描述 投票:0回答:1

我想在dataframe中重命名我的列。虽然我在dplyr下使用了一个准备使用的简单重命名功能,但是我收到一条错误信息,如下所示。不确定是什么错误。你能帮我么?

enter image description here

我有多列,但我想只将'operator_codes'重命名为'operator_concept_id',将'value_codes'重命名为'value_concept_id'。

oper_val_concepts = function(DF){
  DF %>%
    mutate(Symbol = str_extract(.$value,"[^.\\d]*")) -> df_ope
 key <- data.frame(Symbol = c("",">","<","-","****","inv","MOD","seen"),
  operator_codes 
     =c(4172703L,4172704L,4171756L,4172703L,0L,0L,0L,0L),
  value_codes=c(45884084L,45876384L,45881666L,
         45878583L,45884086L,45884086L,45884086L,45884086L)) 
dfm <-merge(x=df_ope,y=key,by="Symbol",all.x = TRUE)
dfm %>%
   rename(operator_concept_id=operator_codes,value_concept_id=value_codes)
   #select (-Symbol)
 }

我希望输出数据帧具有重命名的列标题,但我得到一条错误消息,如上所示。你能告诉我这是什么错误吗?我无法共享数据,因为它是保密的。

r dataframe dplyr multiple-columns rename
1个回答
0
投票

假设df是包含“operator_codes”,“value_codes”作为列的数据框的名称。您可以将这些列名更改为新名称,如下所示:

重命名R中的数据框列:

colnames(df)[colnames(df)=="operator_codes"] <- "operator_concept_id"
colnames(df)[colnames(df)=="value_codes"] <- "value_concept_id"
© www.soinside.com 2019 - 2024. All rights reserved.