sub-dict: Extract or Replace Parts of a dict

Description Usage Arguments Examples

Description

Extract or Replace Parts of a dict

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## S3 method for class 'dict'
x[i, j]

## S3 method for class 'dict'
x[[i, j]]

## S3 replacement method for class 'dict'
x[i, j] <- value

## S3 replacement method for class 'dict'
x[[i, j]] <- value

## S3 replacement method for class 'dict'
x$name <- value

Arguments

x

Object of class dict.

i, j

Indices specifying elements to replace.

value

Values to insert into dict.

name

Character string or a name.

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
# Extracting keys
d <- dict(x = 1, y = 2)
d["x"]
d["z"] <- 3

# Vectorized assignments
new_keys <- c("one", "two")
new_vals <- c(1, 2)
d[new_keys] <- new_vals

d[[letters]] <- LETTERS
d[["a"]]
d[["B"]]

# Vectorized extractions
keys_to_extract <- c("x", "two")
d[keys_to_extract] # return named list
d[[keys_to_extract]] # returns nameless list

## Not run: 
d <- dict()
d$x <- 1 # will throw an error
d$x # NULL

d[1] <- 1 # not allowed - key has to be a character
d["a"] <- c(1, 2) # will assign c(1, 2) to name 'a'
d[c("a", "b")] <- c(1, 2) # will assign 1 to 'a' and 2 to 'b'
d[c("a", "b", "c")] <- c(1, 2) # will throw an error

## End(Not run)

skubicius/dictionary documentation built on May 7, 2019, 7:17 p.m.