Reputation: 7743
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.
Upvotes: 0
Views: 1823
Reputation: 853
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"
Upvotes: 2