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?
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.
or
a <- "c(123, 234, 235, 3, 5, 6)"
eval(parse(text = a))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With