R 函数的维数

R 函数的维数,r,function,dimensions,R,Function,Dimensions,因为这只会稍微简化我的代码,所以我主要是出于好奇。假设你有一个函数 f <- function(x) { c(x[1] - x[2], x[2] - x[3], x[3] - x[1]) } 这里有一个非常值得怀疑的解决方案 我已经编写了三个函数,允许基于某种“模板”解析树片段匹配、搜索和提取解析树片段。这是: ptlike <- function(lhs,rhs,wcs='.any.',wcf=NULL) if (is.symbol(rhs) &&am

因为这只会稍微简化我的代码,所以我主要是出于好奇。假设你有一个函数

f <- function(x) {
  c(x[1] - x[2],
    x[2] - x[3],
    x[3] - x[1])
}

这里有一个非常值得怀疑的解决方案

我已经编写了三个函数,允许基于某种“模板”解析树片段匹配、搜索和提取解析树片段。这是:

ptlike <- function(lhs,rhs,wcs='.any.',wcf=NULL) if (is.symbol(rhs) && as.character(rhs)%in%wcs) !is.function(wcf) || isTRUE(wcf(lhs,as.character(rhs))) else typeof(lhs) == typeof(rhs) && length(lhs) == length(rhs) && if (is.call(lhs)) all(sapply(seq_along(lhs),function(i) ptlike(lhs[[i]],rhs[[i]],wcs,wcf))) else lhs == rhs;
ptfind <- function(ptspace,ptpat,wcs='.any.',wcf=NULL,locf=NULL,loc=integer(),ptspaceorig=ptspace) c(list(),if (ptlike(ptspace,ptpat,wcs,wcf) && (!is.function(locf) || isTRUE(locf(ptspaceorig,loc)))) list(loc),if (is.call(ptspace)) do.call(c,lapply(seq_along(ptspace),function(i) ptfind(ptspace[[i]],ptpat,wcs,wcf,locf,c(loc,i),ptspaceorig))));
ptextract <- function(ptspace,ptpat,gets='.get.',wcs='.any.',wcf=NULL,locf=NULL,getf=NULL) { getlocs <- do.call(c,lapply(gets,function(get) ptfind(ptpat,as.symbol(get),character()))); if (length(getlocs)==0L) getlocs <- list(integer()); c(list(),do.call(c,lapply(ptfind(ptspace,ptpat,unique(c(gets,wcs)),wcf,locf),function(loc) do.call(c,lapply(getlocs,function(getloc) { cloc <- c(loc,getloc); ptget <- if (length(cloc)>0) ptspace[[cloc]] else ptspace; if (!is.function(getf) || isTRUE(getf(if (missing(ptget)) substitute() else ptget,loc,getloc,as.character(ptpat[[getloc]])))) list(if (missing(ptget)) substitute() else ptget); }))))); };
ptfind()。例如:

ptlike(1L,2L);
## [1] FALSE
ptlike(1L,1L);
## [1] TRUE
ptlike(quote(a),quote(b));
## [1] FALSE
ptlike(quote(a),quote(a));
## [1] TRUE
ptlike(quote(sum(a+1)),quote(sum(b+1)));
## [1] FALSE
ptlike(quote(sum(a+1)),quote(sum(.any.+1)));
## [1] TRUE
ptlike(quote(sum(a+1)),quote(.any.(a+1)));
## [1] TRUE
ptlike(quote(sum(a+1)),quote(.any.));
## [1] TRUE
sp <- quote({ a+b*c:d; e+f*g:h; });
ptfind(sp,quote(.any.:.any.));
## [[1]]
## [1] 2 3 3
##
## [[2]]
## [1] 3 3 3
##
sp[[c(2L,3L,3L)]];
## c:d
sp[[c(3L,3L,3L)]];
## g:h

因此,我们可以做的是从(包含函数体的解析树)函数中提取
x
参数的所有下标,并获得最大文字下标值:

f <- function(x) c(x[1L]-x[2L],x[2L]-x[3L],x[3L]-x[1L]);
max(unlist(Filter(is.numeric,ptextract(body(f),quote(x[.get.])))));
## [1] 3

f我认为函数没有维度。数组可以有一个。为什么你的函数应该有dim 3?根据输入,输出向量可以是每个整数。不清楚我们为什么需要这个。可能在函数内部输入验证,如果长度不是3,则不要运行,等等@Alex如果我只输入2dim,该函数不会抛出错误吗?@zx8754说您有很多不同的函数,比如上面的函数,它们具有不同的输入需求,现在,您需要确保为每个函数选择了正确的输入。您可以使用
x[i]
来子集什么。只有向量?
ptextract(sp,quote(c:d));
## [[1]]
## c:d
##
ptextract(sp,quote(.any.:.any.));
## [[1]]
## c:d
##
## [[2]]
## g:h
##
ptextract(sp,quote(.any.:.get.));
## [[1]]
## d
##
## [[2]]
## h
##
f <- function(x) c(x[1L]-x[2L],x[2L]-x[3L],x[3L]-x[1L]);
max(unlist(Filter(is.numeric,ptextract(body(f),quote(x[.get.])))));
## [1] 3