我想在一个字符串中创建一个关联数组"key1=values1;key2=value2".我知道这可以通过双重拆分和手动构建阵列来完成,但我想知道是否已经可以使用的东西.
Jos*_*ien 10
使用环境作为"关联数组"提供了一种简单的解决方案.
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
Run Code Online (Sandbox Code Playgroud)