本文介绍了我如何找到R中函数列表的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 为了创建一个函数列表,我想创建一个for循环,它返回(显然是一个函数)它们的总和。一个for循环中的函数列表,我使用这段代码 ##代码 f = dnorm h = function(x){log(f(x))} S = c(-3,-2,-1,0,1,2,3)$ b $对于(i中1:长度(S)){ K [i,] b =矩阵(rep(1:length(S),2),ncol = 2) = c(S [i],h(S [i]))} funcs = list() ## LOOP来定义LINES (i in 1:6){ ##使函数名称 funcName< - paste('hl',i,i + 1,sep ='') ## Make function func1 = paste('function(x){(K [',i''+ 1,2] -K [',i, ',2])/(K [',i,'+ 1,1] -K [',i'',1])* x + K [',i''+ 1,2] - ((K [ 'I, '+ 1,2] -K [',I '2])/(K [',I, '+ 1,1] -K [',I',1 ]))* K [',i''+ 1,1]}',sep ='') funcs [[funcName]] = eval(parse(text = func1 )) } whi ch创建了6个函数的列表。我怎样才能得到他们的总和?我尝试使用apply命令,但是我的语法不正确,或者它们不工作。 PS我正在尝试为ars命令编写一个代码。 / p> 解决方案 正如尼克指出的那样,函数的总和是没有意义的。我疯狂地猜测你想在某个时候在函数中进行评估(在 S ?),然后取这些值的总和。这应该是诀窍。 $ $ $ $ $ $ $ $ $ $ $ $ $ 大部分代码可以更干净地编写, f h S K i funcNames #你可以避免在这个函数中使用`paste` /`eval` /`parse`来创建函数 #Can可能通过使用本地更干净地完成makeFunc< - 函数(i) { evalq(替代(函数(x) {(K [i + 1,2] (K [i,1])/(K [i + 1,1] -K [i,1])* x + K [i + 1,2] - ,K [i,2])/(K [i + 1,1] (i = i)))} funcs< - lapply(i,makeFunc)名称(funcs)< - funcNames rowSums(sapply(funcs,function(f)f(S))) So i have a list of functions.I want to create a for loop that returns (obviously as a function) the sum of them.In order to create a list of functions inside a for loop i am using this code##CODEf=dnormh=function(x){log(f(x))}S=c(-3,-2,-1,0,1,2,3)K=matrix(rep(1:length(S),2),ncol=2)for(i in 1:length(S)){ K[i,]=c(S[i],h(S[i])) }funcs=list()## LOOP TO DEFINE THE LINESfor(i in 1:6){## Make function namefuncName <- paste( 'hl', i,i+1, sep = '' )## Make functionfunc1 = paste('function(x){ (K[',i,'+1,2]-K[',i,',2])/(K[',i,'+1,1]-K[',i,',1])*x+K[',i,'+1,2]-((K[',i,'+1,2]-K[',i,',2])/(K[',i,'+1,1]-K[',i,',1]))*K[',i,'+1,1]}',sep= '')funcs[[funcName]] = eval(parse(text=func1)) }which creates a list of 6 functions. How can I get their sum? I tried using the apply commands but either my syntax is not correct or they do not work.P.S I am actually trying to write my one code for the ars command. 解决方案 As Nick pointed out, "the sum of functions" doesn't make sense. I'm wildly guessing that you want to evaluate at function at some point (at S?) and then take the sum of those values. This should do the trick.rowSums(sapply(funcs, function(f) f(S)))Much of your code can be written more cleanly, and in a vectorised way.f <- dnormh <- function(x) log(f(x))S <- -3:3K <- cbind(S, h(S)) #No need to define this twice; no need to use a loopi <- seq_len(6)funcNames <- paste('hl', i, i+1, sep = '') #paste is vectorised#You can avoid using `paste`/`eval`/`parse` with this function to create the functions#Can possibly be done even more cleanly by using localmakeFunc <- function(i){ evalq(substitute( function(x) { (K[i + 1, 2] - K[i, 2]) / (K[i + 1, 1] - K[i, 1]) * x + K[i + 1, 2] - ((K[i + 1, 2] - K[i, 2]) / (K[i + 1, 1] - K[i, 1])) * K[i + 1, 1] }, list(i = i) ))}funcs <- lapply(i, makeFunc)names(funcs) <- funcNamesrowSums(sapply(funcs, function(f) f(S))) 这篇关于我如何找到R中函数列表的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 13:14