Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing column name – replace the prefix

Suppose I have the dataframe below:

>colnames(df)

>[1] "0939.HK.Open"     "0939.HK.High"     "0939.HK.Low"      "0939.HK.Close"    "0939.HK.Volume"   "0939.HK.Adjusted"

Wondering if I can replace column name with a defined symbol?

>symbol<-'0123.KI' 

And what I would like to obtain is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>[1] "0123.KI.Open" "0123.KI.High" "0123.KI.Low" "0123.KI.Close" "0123.KI.Volume" "0123.KI.Adjusted"

Thank you so much guys.

>Solution :

Try:

colnames(df) <- paste0(symbol, ".", tools::file_ext(colnames(df)))

As the column names look like a filename, I am taking the extension – file_ext, then adding the symbol using paste0.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading