Home > other >  How to put single quotation marks around comma-separated numbers in a character column of a data fra
How to put single quotation marks around comma-separated numbers in a character column of a data fra

Time:06-16

I have a data frame with a character column that contains comma-separated numbers. How can I put each comma-separated number into single quotation marks?

Consider the illustrative example below. Basically, I would like to transform data frame df1 into data frame df2.

var1 <- c("1,2,3","1,24,41")
df1 <- data.frame(var1)
df1
##      var1
## 1   1,2,3
## 2 1,24,41

var1 <- c("'1','2','3'","'1','24','41'")
df2 <- data.frame(var1)
df2
##            var1
## 1   '1','2','3'
## 2 '1','24','41'

I found a question on Stack Overflow which addresses a similar question (Put quotation marks around each element of a vector, and separate with comma). However, it is about a character vector. I can't figure out how to apply the solution in this case (or any other solution that might exist) to a character column of a data frame.

CodePudding user response:

As you have a full character string with the numbers, you cannot use the solutions you refer to as they start of with numeric vectors. Instead, you'll need to manipulate the strings. Here I look for numbers and replaces each match with itself in single quotes.

var1 <- stringr::str_replace_all(var1, "\\d ", "'\\0'")

or in your data frame:

df1$var2 <- stringr::str_replace_all(df1$var1, "\\d ", "'\\0'")

Output:

> df1
     var1          var2
1   1,2,3   '1','2','3'
2 1,24,41 '1','24','41'

Edit: Bug/typo.

CodePudding user response:

Here is the answer using your approach

df1$var1 <- lapply(strsplit(df1$var1 , ",") ,
            function(x) paste0(sprintf("'%s'", x), collapse = ", "))
  •  Tags:  
  • r
  • Related