I am trying to write a function that will take a column from a dataframe, and replace some commas with semicolons. Taking the example of a value below (i.e., 1 cell from a dataframe's column)
Orange (carrot, orange), Blue (sky, ball), Yellow (lemon, boots)
Turned into
Orange (carrot, orange); Blue (sky, ball); Yellow (lemon, boots)
I've created the function below, but 1) it does not like the current regex (continue to get errors) and 2) it replaces all values with NA (there are some values that have NA, and I'd like to keep them as such, only replacing values with data in the cells).
semicolon <- function(x) {
as.double(gsub(",(?=[A-Z])",";", x))
}
I am trying to write a function that will take a column from a dataframe, and replace some commas with semicolons. Taking the example of a value below (i.e., 1 cell from a dataframe's column)
Orange (carrot, orange), Blue (sky, ball), Yellow (lemon, boots)
Turned into
Orange (carrot, orange); Blue (sky, ball); Yellow (lemon, boots)
I've created the function below, but 1) it does not like the current regex (continue to get errors) and 2) it replaces all values with NA (there are some values that have NA, and I'd like to keep them as such, only replacing values with data in the cells).
semicolon <- function(x) {
as.double(gsub(",(?=[A-Z])",";", x))
}
Share
Improve this question
asked Mar 27 at 18:25
Kayla SchouKayla Schou
1
3
|
1 Answer
Reset to default 3You need to use a lookbehind to find commas after close parens ("(?<=\\))"
) -- your current approach ("(?=[A-Z])"
) uses a lookahead to find commas immediately before a capital letter. You also need to set perl = TRUE
to use lookarounds. And it's not clear why you're including as.double()
, which will try to coerce the character string to a number and return NA
. So:
semicolon <- function(x) {
gsub("(?<=\\)),", ";", x, perl = TRUE)
}
x <- "Orange (carrot, orange), Blue (sky, ball), Yellow (lemon, boots)"
semicolon(x)
# "Orange (carrot, orange); Blue (sky, ball); Yellow (lemon, boots)"
Edit: As @TimG pointed out in a comment, you don't need the lookbehind if you include the close paren in the replacement:
semicolon <- function(x) gsub("\\),", ");", x)
semicolon(x)
# "Orange (carrot, orange); Blue (sky, ball); Yellow (lemon, boots)"
as.double()
? Also in this case, no negative lookaheads are needed, you can also usegsub(")\\, ", ")\\; " , "Orange (carrot, orange), Blue (sky, ball), Yellow (lemon, boots)")
– Tim G Commented Mar 27 at 18:59