• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to remove first set of numbers from column names?

Resolved: How to remove first set of numbers from column names?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have column names, for example they look like this 20819830_r1ar_u_stationary and 2081974_f8ar_u. I am trying to get rid of the first set of numbers in the column names. I tried using this code

names(df)[1:2] <- gsub("^.*_","",names(df[,c(1:2)])) [/code]

but when I use this, the column names turn to stationary and u. I can see the code is removing everything up until the last _ how do I change the code so that it removes everything up until the first _.

Answer:

Instead of matching .* – one or more characters as . matches any characters, it should be one or more digits (\\d+) from the start (^) of the string

names(df)[1:2] <- sub("^\\d+_", "", names(df)[1:2]) [/code]

If you have better answer, please add a comment about this, thank you!

gsub r
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Getting ‘502 Bad Gateway’ while deploying Springboot app in EKS

24/03/2023

Resolved: Why is NGINX’s $request_uri empty?

24/03/2023

Resolved: How to convert Java bytecode to Webassembly using CheerpJ compiler

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.