Home > OS >  R: How do I add a hyphen to all the variables in a column?
R: How do I add a hyphen to all the variables in a column?

Time:03-18

I want to add a hyphen - (after the second letter) to all the variables in the FID column of the d dataframe. For example, if the variable is AC10, I want to change it to AC-10 and likewise AC11 to AC-11 for the entire column.

My attempt:

d$FID <- sub("^(\D )(\d ).*", "-", d$FID)

> dput(d$FID)
c("AC10", "AC11", "AC12", "AC13", "AC14", "AC15", "AC17", "AC18", 
"AC19", "AC1", "AC20", "AC21", "AC22", "AC23", "AC24", "AC25", 
"AC26", "AC27", "AC29", "AC2", "AC30", "AC31", "AC32", "AC33", 
"AC34", "AC35", "AC36", "AC37", "AC38", "AC39", "AC3", "AC40", 
"AC41", "AC42", "AC43", "AC45", "AC46", "AC47", "AC48", "AC49", 
"AC50", "AC51", "AC52", "AC53", "AC54", "AC55", "AC56", "AC57", 
"AC58", "AC5", "AC60", "AC61", "AC62", "AC63", "AC64", "AC65", 
"AC66", "AC67", "AC69", "AC6", "AC70", "AC71", "AC72", "AC73", 
"AC74", "AC75", "AC76", "AC77", "AC78", "AC79", "AC7", "AC80", 
"AC81", "AC82", "AC83", "AC84", "AC86", "AC87", "AC88", "AC89", 
"AC8", "AC90", "AC91", "AC92", "AC9", "AC100", "AC101", "AC102", 
"AC103", "AC104", "AC105", "AC16", "AC68", "AC93", "AC94", "AC95", 
"AC96", "AC97", "AC99", "DE10", "DE12", "DE13", "DE14", "DE15", 
"DE16", "DE17", "DE18", "DE19", "DE1", "DE20", "DE21", "DE22", 
"DE23", "DE25", "DE26", "DE27", "DE2", "DE33", "DE34", "DE35", 
"DE36", "DE37", "DE38", "DE39", "DE3", "DE40", "DE41", "DE42", 
"DE44", "DE45", "DE46", "DE47", "DE48", "DE49", "DE4", "DE50", 
"DE51", "DE52", "DE53", "DE54", "DE55", "DE56", "DE57", "DE58", 
"DE59", "DE60", "DE7", "DE9", "DE29", "DE30", "DE32", "DE43", 
"DE5")

CodePudding user response:

You can use

d$FID <- sub("(\\D)(\\d)", "\\1-\\2", d$FID)

See this regex demo.

Here

  • (\D) - Group 1 (\1): any non-digit
  • (\d) - Group 2 (\2): any digit.

You may also use

d$FID <- sub("([[:alpha:]])(\\d)", "\\1-\\2", d$FID)

where [[:alpha:]] matches any letter,.

See this regex demo.

  • Related