one2many: convert one2many and many2one

Description Usage Arguments Details Value TODO Author(s) Examples

Description

convert back and forth between one 2 many data, ie one key vs multiple values

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
n2o(x, sep = " /// ")

o2n(x, sep = " /// ")

o2nu(x, sep = " /// ")

map2o(map, idx1 = 1, idx2 = 2, make.unique = TRUE, sort.method = sort)

map2list(map, idx1 = 1, idx2 = 2, make.unique = TRUE,
  sort.method = sort)

map2env(map, idx1 = 1, idx2 = 2, make.unique = TRUE, sort.method = sort)

list2env(x, envir = NULL)

list2map(x)

Arguments

x

a list of elements, each element being a vector with >=1 value

sep

the field seperator

map

a matrix-like object with >= 2 columns

idx1

which column contains the "keys"

idx2

which column contains the "values"

make.unique

logical: if there are duplicate values for one key, keep the duplicates, or remove them entirely?

sort.method

howto sort the keys. the default is the "sort" function.

envir

an environment, or NULL

Details

n2o: convert many-to-one ie convert a list of vectors into a char vector, with elements in each input vector separated by sep. Useful for displaying one-to-many data in a single column of a table.

o2n: convert one-to-many ie convert a char vector of entries separated by sep into a list of vectors. Useful for displaying one-to-many data in a single column of a table.

o2nu: convert one-to-many ie convert a char vector of entries separated by sep into a list of vectors, and make elements unique. Useful for displaying one-to-many data in a single column of a table.

map2o: convert two columns into a o2n object.

map2list: Convert 2 columns in a n:n map into a 1:n list, such that each unique element in map[,idx1] is a list element, and all of the things that are in the idx2'th column.

map2env: convert a map to an environment. This is more memory efficient than running map2list then list2env. Convert 2 columns in a n:n map into a 1:n list, such that each unique element in map[,idx1] is a list element, and all of the things that are in the idx2'th column.

list2env: convert a named list of keys to values into a hashed environment

list2map: Convert a list, where element names = key, and element vectors are the values into a 2 column map.

Value

n2o: a character vector of values, where elements that had >1 value are separated by sQuotesep

o2nu: a list of vectors, where each vector has just one value

map2o: a 2 column data.frame with the keys in the first column and the values as n2o in the 2nd column. (ie separated by " /// ").

map2list: A named list of keys to values

map2env: An environment

list2env: a hashed environment

list2map: a 2 column data.frame, with these elements:

keys

search key

value

mapped value

TODO

make it quicker if the map is sorted by the keys.

Author(s)

Mark Cowley, 15 March 2007

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# n2o
x <- list(104009, c(71772, 231691)) 
n2o(x) 
# [1] "104009", "71772 /// 231691"
# o2n
y <- c("104009", "71772 /// 231691")
o2n(y)
# [[1]]
# [1] "104009"
# [[2]]
# [1] "71772" "231691"
y <- c("104009", "71772 /// 71772")
o2n(y)
# [[1]]
# [1] "104009"
# [[2]]
# [1] "71772" "231691"
o2nu(y)
# [[1]]
# [1] "104009"
# [[2]]
# [1] "71772"

# map2o
map <- cbind( c("a", "a", "a", "b", "c"),
              c("1", "2", "2", "1", "4")) 

map2o(map, 1, 2, TRUE)
# "a", "1 /// 2"
# "b", "1"
# "c", "4"
map2o(map, 1, 2, FALSE)
# "a", "1 /// 2 /// 2"
# "b", "1"
# "c", "4"
map2o(map, 1, 2, TRUE, rev)
# "a" , "2 /// 1"
# "b" , "1"
# "c" , "4"
map2o(map, 1, 2, FALSE, rev)
# "a", "2 /// 2 /// 1"
# "b", "1"
# "c", "4"

drmjc/mjcbase documentation built on May 15, 2019, 2:27 p.m.