在R中自动创建和使用自定义函数 [英] Automatic creation and use of custom made function in R

查看:314
本文介绍了在R中自动创建和使用自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在for循环中创建评估不同的索引. 这些索引具有不同的公式,并不总是需要对其进行评估. f.i. : 我要评估的指标可能是

I would like to create evaluate different indexes in a for loop cycle. those indexes has different formulas and not always they need to be evaluated. f.i. : my indices to evaluate might be

a=1
b=2
c=5
d=8
IDX1=function(a,b) {result=a+b}
IDX2=function(c,b) {result=c+b}
IDX3=function(d,b) {result=d+b-c}
IDX4=function(a,d) {result=a+d+b+c}

公式并不重要

在一个数据帧中,我具有迭代次数和每个循环需要获取的索引(假设我必须为每个迭代评估2个索引)

in a data frame I have the iteration number and the indices i need to take at each loop (let's say that I have to evaluate 2 indices for each iteration)

head=c("iter","IndexA","IndexB")
r1=c(1,"IDX1","IDX2")
r2=c(2,"IDX3","IDX4")
r3=c(3,"IDX1","IDX4")
df=as.data.frame(rbind(head,r1,r2,r3))

我想做的是在循环中为每次迭代评估各自的2个索引,并自动调用正确的公式广告并为其提供正确的参数

what I would like to do is within the loop evaluate for each iteration the respective 2 indices, calling automatically the right formula ad feed it with the right arguments

iter 1 : IndexA=IDX1(args)=3  ; IndexB(args)=IDX2(args)=7
iter 2 : IndexA=IDX3(args)=5  ; IndexB(args)=IDX4(args)=16
iter 3 : IndexA=IDX1(args)=3  ; IndexB(args)=IDX4(args)=16

请不要回答只需运行所有功能并调出 循环中需要的结果."

Plese do not answer with "just run all the function and recall the needed result in the loop".

我正在处理大型矩阵,而内存确实是一个问题.我需要评估循环中的功能以减少内存使用量

I'm working with big matrix and memory is a problem indeed. I need to evaluate the function within the loop to reduce the usage of memory

我相信答案是本次讨论的内容,但我无法理解.

I believe that the answer is some what inside this discussion but I can't get trough.

如何以编程方式创建R函数?

有人可以解释一下我吗 1.如何构建可以循环编程更改的功能? 2.一旦有了它,如何运行公式并获得所需的结果? 谢谢

Can somebody explain me 1. how to built a function that can be programmatically changed in a loop? 2. once I have it how can I run the formula and get the result I want? thanks

推荐答案

您可以使用evalparse函数的组合来调用(评估)任何字符串作为代码.首先,您必须构造一个这样的字符串.为此,您可以将索引指定为字符串.例如:IDX1 = "a + b".然后,您可以使用get("IDX1")通过名称获取该值.

You can use a combination of eval and parse function to call (evaluate) any string as code. First, you have to construct such a string. For this, you can specify your indexes as character strings. For example: IDX1 = "a + b". Then, you can get that value by name with get("IDX1").

尝试以下代码:

# Your preparations
a <- 1
b <- 2
c <- 5
d <- 8
IDX1 <- "a + b"
IDX2 <- "c + b"
IDX3 <- "d + b - c"
IDX4 <- "a + d + b + c"


head = c("iter", "IndexA", "IndexB")
r1 = c(1, "IDX1", "IDX2")
r2 = c(2, "IDX3", "IDX4")
r3 = c(3, "IDX1", "IDX4")
df = as.data.frame(rbind(r1, r2, r3))
colnames(df) <- head

# Loop over df with apply
result <- apply(df, 1, function(x){
    # Construct call string for further evaluation
    IndexA_call <- paste("ia <- ", get(x[2]), sep = "")
    IndexB_call <- paste("ib <- ", get(x[3]), sep = "")

    # eval each call string
    eval(parse(text = IndexA_call))
    eval(parse(text = IndexB_call))

    x[2:3] <- c(ia, ib)
    return(as.numeric(x))
})

result <- t(result)
colnames(result) <- head
print(result)

这给出了:

   iter IndexA IndexB
r1    1      3      7
r2    2      5     16
r3    3      3     16

这篇关于在R中自动创建和使用自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆