Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove "c(" and ")" from string in R - elegant solution?

Tags:

regex

sqlite

r

for saving a x,y matrix from R to sqlite, I have to convert it to a list:

# convert matrix to list
sql_peaks <- list(peaks)

The resulting string looks like

"c(123, 234, 235, 3, 5, 6)"

The constructor elements c() are disturbing in further data processing steps (and unnecessary), so I remove them with:

# remove formatting characters
sql_peaks <- gsub("c(", "", sql_peaks, fixed="TRUE")
sql_peaks <- gsub(")", "", sql_peaks, fixed="TRUE")

resulting in:

123, 234, 235, 3, 5, 6

This works fine, but is there no more elegant solution?

like image 996
Robert Winkler Avatar asked Feb 05 '23 07:02

Robert Winkler


2 Answers

Two options come to mind using base R:

x <- "c(123, 234, c(235), 3, 5, 6)"

(I added the c(234) in the middle in order to show that the regex only affects the first/last chars in the string.)

gsub("^c\\(|\\)$", "", x)
# [1] "123, 234, c(235), 3, 5, 6"
substr(x, 3, nchar(x) - 1)
# [1] "123, 234, c(235), 3, 5, 6"

Between the two, as long as you are certain that it will always have the leading c( and trailing ), I'd go with the latter. It is also a little faster:

library(microbenchmark)
microbenchmark(a=gsub("^c\\(|\\)$", "", x), b=substr(x, 3, nchar(x) - 1))
# Unit: microseconds
#  expr   min    lq     mean median     uq    max neval
#     a 7.294 7.659 10.20421  8.024 9.4825 46.314   100
#     b 1.459 1.824  2.41480  1.824 2.1890 17.140   100

If you are looking for something aesthetic, then this may be your solution.

However, if you are looking for a robust way to store variable and extractable vectors of data from a single database cell, might I suggest using something like JSON?

y <- c(123, 234, 235, 3, 5, 6)
jsonlite::toJSON(y)
# [123,234,235,3,5,6] 
jsonlite::fromJSON("[123,234,235,3,5,6]")
# [1] 123 234 235   3   5   6

It's robust in that it will handle different types, lengths, etc, rather eloquently. (Also, many DBMS accept json and jsonb data types.) Frankly, there are other serializing methods out there, JSON just happened to be first on my mind.

like image 73
r2evans Avatar answered Feb 07 '23 01:02

r2evans


or

a <- "c(123, 234, 235, 3, 5, 6)"
eval(parse(text = a))
like image 44
Hao Avatar answered Feb 06 '23 23:02

Hao