Learn R Programming

PivotalR (version 0.1.18.3.1)

arraydb.to.arrayr: Convert strings extracted from database into arrays

Description

An array object in database is converted to a string when passed into R, for example '\{1.2, 3.4, 5.7\}', and this function can convert the string to an array in R, for example c(1.2, 3.4, 5.7). This function can also convert a vector of such strings into a two-dimensional array.

Usage

arraydb.to.arrayr(str, type = "double", ...)

Arguments

str

A vector of strings, or a single string, that has multiple elements in it and deliited by ",".

type

The type of the return value of this function. Default is "double". It can be "character", "double", "logical", "integer", "numeric" etc. All types other than "character", "logical" and "integer" will be treated as "numeric".

Further arguments passed to or from other methods. Currently, no more parameters can be passed and this is kept for backwards compatibility.

Value

A two dimensional array, whose element's type is decided by the function argument type.

Details

When R reads in data from a table in the database, the result is a data.frame object. However, if the orginal data table has a column which is the array type, the array is automatically converted into a string and data.frame object has a corresponding column of strings, each of which starts with "\{" and ends with "\}", and all the original array elements are casted into strings delimited by ",".

For example, the array in database array['ab', 'c d', '"axx, t"'] becomes a string in R '{ab, c d, \"axx, t\"}'.

This function deals with such strings and turn them into faimiliar arrays that users can directly use.

See Also

lk or link{lookat} extracts the data of a table

Examples

Run this code
# NOT RUN {
<!-- %% @test .port Database port number -->
<!-- %% @test .dbname Database name -->
## set up the database connection
## Assume that .port is port number and .dbname is the database name
cid <- db.connect(port = .port, dbname = .dbname, verbose = FALSE)

## Example 1 ----------

str <- '{1.2, 3.4, 5.6}'
arraydb.to.arrayr(str, "double") # c(1.2, 3.4, 5.6)

str <- '{a, b, "c, d"}'
arraydb.to.arrayr(str, "character") # c("a", "b", "\"c, d\"")

## Example 2 ----------

## table_in_database has a column of arrays
x <- as.db.data.frame(abalone, conn.id = cid, verbose = FALSE)
x$col.array <- db.array(x[,3:10])
dat <- lk(x, nrows = 50, array = FALSE) # extract the actual data
arraydb.to.arrayr(dat$col.array, "double") # an array of 50 rows

## ----------------------------------------------------------------------
db.disconnect(cid, verbose = FALSE)
# }

Run the code above in your browser using DataLab