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

Description Usage Arguments Details Value Note Author(s) See Also Examples

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

1
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.

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.

Value

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

Note

(1) The returned value is a two dimensional array, even if str is a single string.

(2) Although this function is for the strings extracted from database, it can actually deal with strings like "a, b, c", which do not start or end with curly brackets.

Author(s)

Author: Predictive Analytics Team at Pivotal Inc.

Maintainer: Frank McQuillan, Pivotal Inc. fmcquillan@pivotal.io

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Not run: 


## 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)

## End(Not run)

PivotalR documentation built on March 13, 2021, 1:06 a.m.