r - Remove unwanted hyphen from variable -
i have data 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 way remove initial unwanted hypen , make 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 helpful.
we can use sub
remove unwanted -
@ 2nd position of string. use ^
specify start of string, capture first character group ((.)
- .
metacharacter refers character element) followed -
, in replacement use backreference (\\1
) 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"
data
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))
Comments
Post a Comment