Here is a super short example from the dataframe that I'm using.
structure(list(billing_postcode = c("34990", "34695-4809")), row.names = c(NA,
-2L), class = "data.frame")
How do I remove all of the numbers after the hyphen, including the hyphen? I'm working with a column of zip codes and just want the first five numbers all the way through. Although, there may be three-digit zip codes in here also, so I'm looking for a solution that recognizes the hyphen symbol. I know this pattern:
gsub("-", "", df$billing_codes)
but this just removes the hyphen, and not everything after it, also
CodePudding user response:
To remove the hyphen in any case, even if it's not followed by anything use -.*
- ‘*’ The preceding item will be matched zero or more times.
- ‘ ’ The preceding item will be matched one or more times.
df
billing_postcode
1 34990
2 34695-4809
3 34695-
gsub("-.*", "", df$billing_postcode)
[1] "34990" "34695" "34695"
Data
df <- structure(list(billing_postcode = c("34990", "34695-4809", "34695-"
)), row.names = c(NA, -3L), class = "data.frame")