Reputation: 75
I have data as below
0-0098-45.3A-22
0-0104-44.0A-23
0-0983-29.1-22
0-1757-42.5A-22
0-4968-37.3A2-23
000000-44.0a-23
000000-45.3A-42
is there any way to remove initial unwanted hypen and make it like below in R
00098-45.3A-22
00104-44.0A-23
00983-29.1-22
01757-42.5A-22
04968-37.3A2-23
000000-44.0a-23
000000-45.3A-42
Any help would be much helpful.
Upvotes: 1
Views: 159
Reputation: 887291
We can use sub
to remove the unwanted -
at 2nd position of the string. We use ^
to specify the start of the string, capture the first character as a group ((.)
- .
is a metacharacter which refers to any character element) followed by -
and in the replacement we use the backreference (\\1
) for that captured group.
df$v1 <- sub('^(.)-', '\\1', df$v1)
df$v1
#[1] "00098-45.3A-22" "00104-44.0A-23" "00983-29.1-22"
#[4] "01757-42.5A-22" "04968-37.3A2-23" "000000-44.0a-23" "000000-45.3A-42"
df <- structure(list(v1 = c("0-0098-45.3A-22", "0-0104-44.0A-23", "0-0983-29.1-22",
"0-1757-42.5A-22", "0-4968-37.3A2-23", "000000-44.0a-23", "000000-45.3A-42"
)), .Names = "v1", class = "data.frame", row.names = c(NA, -7L))
Upvotes: 2