0

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?

enter image description here

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.

5
  • Does DF on which you apply the function contains columns named operator_codes and value_codes ? Commented Apr 15, 2019 at 9:46
  • 3
    Try dplyr::rename. Commented Apr 15, 2019 at 9:50
  • 1
    This worked. Thank you @NelsonGon Commented Apr 15, 2019 at 9:51
  • @fmarm - Yes, the columns were present. The trick dplyr::rename suggested above worked. Guess there was same function in another package as well Commented Apr 15, 2019 at 9:52
  • colnames(df) <- c("a","b","c") Commented Apr 15, 2019 at 12:15

1 Answer 1

2

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" 
Sign up to request clarification or add additional context in comments.

1 Comment

can help me with this post? stackoverflow.com/questions/62698244/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.