Other languages (e.g. Python) have a native,shorthand way to reference an object's key or values while iterating. R does not. This is where kv steps in.
kv provides a simple/no magic mechanism for key-value-index iteration. It is best illustrated by the simple example:
mylist = list( a=1, b=2, c=3) # List to iterate over
for( . in kv(mylist) )
cat( "key is: ", .$k, "| value is:", .$v, "\n" )
} kv works by transforming its argument into a list-of-lists, each element
having the appropriate k and v. See ?kv for details.
NB: the use of . is a convention, any valid R name will work in its place.
There are also additional funtions:
kvi(): uses keys, values and indexeskv(): uses keys and valueski(): uses keys and indexesvi(); uses values and indexes
kv also allows for iteration over rows in a table, for example:
for( . in each_row(iris) )
cat( .$Species, ": ", .$Sepal.Lenght )Latest Release:
install.packages('kv') # Soon
Development Version:
install.packages('devtools')
devtools::install_github('decisionpatterns/kv')
The function of this packages are permutation of letters 'k', 'v' and 'i'. Each function turns its argument into a list-of-lists with the corresponding (k)ey, (v)alue and/or (i) set.
The use of . is a simple convention that allows use to use the concise
syntak .$k, .$v or .$i to access the element.