简体   繁体   中英

Rename a column in dataframe using R

I would like to rename my columns in dataframe. Though I am using a ready to use simple rename function under dplyr, I get an error message as shown below. Not sure what is the mistake. Can you please help me?

在此输入图像描述

I have multiple columns but I would like to rename only the 'operator_codes' to 'operator_concept_id' and 'value_codes' to '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)
 }

I expect the output dataframe to have the renamed column headings but I get an error message as shown above. Can you please let me know what is the mistake? I can't share the data as it's confidential.

Suppose df is the name of your data frame containing "operator_codes","value_codes" as columns. You can change these column names to a new names as below:

Rename a data frame column in R:

colnames(df)[colnames(df)=="operator_codes"] <- "operator_concept_id"
colnames(df)[colnames(df)=="value_codes"] <- "value_concept_id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM