Connor Murray
Connor Murray

Reputation: 313

Adding a period between characters in a column in R

species <- c("Dacut","Hhyde","Faffi","Dmelan","Jrobusta")
leg <- c(1,2,3,4,5)
df <- data.frame(species, leg)

I am trying to add a period (".") between the first and second letter of every character in the first column of a data frame.

#End Goal:
#D.acut
#H.hyde
#F.affi
#D.melan
#J.robusta

Does anyone know of any code I can use for this issue?

Upvotes: 2

Views: 1770

Answers (2)

JBGruber
JBGruber

Reputation: 12410

Using substr() to split the string at the positions:

species <- c("Dacut","Hhyde","Faffi","Dmelan","Jrobusta")
leg <- c(1,2,3,4,5)
df <- data.frame(species, leg, stringsAsFactors = FALSE)


df$species <- paste0(
  substr(df$species, 1, 1),
  ".",
  substr(df$species, 2, nchar(df$species))
)
df$species

the first substr() extracts character 1 to 1, the second extracts character 2 to last character in string. With paste() we can put the . in between.

Or sub() with a back-reference:

df$species <- sub("(^.)", "\\1.", df$species)

(^.) is the first character in the string grouped with (). sub() replaces the first instance with the back-refernce to the group (\\1) plus the ..

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520938

Using sub, we can find on the zero-width lookbehind (?<=^.), and then replace with a dot. This has the effect of inserting a dot into the second position.

df$species <- sub("(?<=^.)", "\\.", df$species, perl=TRUE)
df$species

[1] "D.acut"    "H.hyde"    "F.affi"    "D.melan"   "J.robusta"

Note: If, for some reason, you only want to do this replacement if the first character in the species name be an actual capital letter, then find on the following pattern instead:

(?<=^[A-Z])

Upvotes: 1

Related Questions