Associative array from string

I would like to create an associative array in R from a string like "key1=values1;key2=value2". I know this can be done by double splitting and building the array manually but i was wondering if there's already something i can work with.

Answers


Using an environment as the "associative array" provides a straightforward solution.

string <- "key1=99; key2=6"

# Create an environment which will be your array
env <- new.env()

# Assign values to keys in the environment, using eval(parse())
eval(parse(text=string), envir=env)

# Check that it works:
ls(env)
# [1] "key1" "key2"
env$key1
# [1] 99

as.list(env)
# $key1
# [1] 99

# $key2
# [1] 6

Need Your Help

Duplicate file when deploying apk

android maven-2 jetty

This problem seems to come up for a few people, but I haven't been able to apply the solution suggested in other threads for one reason or another so...

Reliable way to pass Object variable to method that expects Object parameter

vb.net object struct boxing

The normal expected semantics of a reference type is that it should behave as an object identifier. If some variable holds a reference to the 5483rd object a program created, passing that variable...