A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/five-dots/Dict below:

five-dots/Dict: R6 Based Key-Value Dictionary Implementation

Dict is a R package which implements a key-value dictionary data structure based on R6 class. It is designed to be similar usages with other languages' dictionary implementations (e.g. Python).

R's vector and list, of course can have names, so you can get and set value by a name (key) like a dictionary. Using regular data structure must be a recommended way in the most of cases. But, if you are interested in the following characteristics, this package is for you!

install.packages("Dict")

# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("five-dots/Dict")

To instantiate a dict, you can pass any length of key-value pairs to the initialize method.

library(Dict)
ages <- Dict$new(
  Charlie = 40L,
  Alice = 30L,
  Bob = 25L,
  .class = "integer",
  .overwrite = TRUE
)
ages
# A tibble: 3 x 2
 key      value    
 <chr>    <list>   
1 Charlie <int [1]>
2 Alice   <int [1]>
3 Bob     <int [1]>

A value can be access by both Dict$get() or `[` with a character key or integer index of items rows.

ages["Bob"]
ages$get("Bob")
ages$get(3L)

If no key found, value of default is returned.

ages["Michael", default = 30]

Adding a item also can be done by R6 methods Dict$add() or `[<-`.

ages["John"] <- 18L # or ages$add(John = 18L)
ages["John"]

Can be overridden if .overwrite = TRUE (default).

ages["Bob"] <- 26L
ages$get("Bob")

Remove item:

Check if items contains a key:

Sort by keys:

# A tibble: 3 x 2
 key      value    
 <chr>    <list>   
1 Alice   <int [1]>
2 Charlie <int [1]>
3 John    <int [1]>

Clear items:

# A tibble: 0 x 2
# … with 2 variables: key <chr>, value <list>

Fields:

ages$keys
ages$values
ages$items
ages$length

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4