如何在[子集中传递/使用字符串 [英] How to pass/use string in [ to subset
问题描述
如何将 [
中的字符串传递/使用到子集,例如数组。我一直在考虑这样的事情(对于4个dims数组):
How to pass/use string in [
to subset e.g. array. I've been thinking about something like this (for 4 dims array):
inputDims <- ",,'CCC',"
outputArray[parse(text=inputDims)]
以上没有行不通-如何实现?
Above doesn't work - how to achieve this?
我对在 [
-如果可能的话,仅输入字符串(以示例中的形式表示)。
I am not interested in using logical vector (or matrix) inside [
- just string (in a form like it is in the example) if this is possible.
推荐答案
(这似乎是一个
使用<$ c $可能会弄清楚构建R函数调用所需的内容。 c>扫描创建适当长度的字符向量。然后将其附加到列表中,其中数组是第一个元素。需要将空位置转换为TRUE,以使切片成功:
Use scan
to create a character vector of the proper length. Then append it into a list where the array is the first element. Need to convert the "empty" positions into TRUE, to get the slicing to succeed:
vec <- scan( text= inputDims, sep="," , what="")
arglist <- list(outputArray)
arglist[ 2:(length(vec)+1) ] <- as.list(vec)
arglist[ arglist==""] <- TRUE
# Using your example in the other question
> do.call("[", arglist )
bb bbb
a1 NA NA
a2 NA NA
a3 NA NA
您之前曾提到过abind :: asub,如果您想了解它对其参数进行的体操(与您的问题不太相似),那么使用加载的软件包执行以下操作:
You were earlier referred to abind::asub, and if you ant to see what gymnastics it does with its arguments (that are not sufficiently similar to your problem) then do this with the package loaded:
getAnywhere( asub.default )
这篇关于如何在[子集中传递/使用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!