在循环R中迭代时从函数返回值 [英] Return value from function when iterating in a loop R

查看:363
本文介绍了在循环R中迭代时从函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经远离了R中的函数,但决定这是更好的练习。现在我有这个问题。我写我的函数:

  myFunction< -function(tab){#takes表格输入
inP< -c( )
for(x in 1:dim(tab)[1]){{#iterate over rows
rname< -rownames(tab)[x] #rownames as output
if(rname< 5)inP< -c(inP,rname)#trivial work
}
return(inP)#返回在
以上产生的向量

tablist< -as .list(paste(tab,1:4,sep =))
for(x in 1:length(tablist)){
tablist [[x]]< -table c(1:10),c(1:10))
}

inPvec< -c()#predefine向量将结果连接到
中(x in 1: (tablist)){#tabs包含多个表格作为列表
myFunction(tablist [[x]])#run myFunction for tabs in tab
inPvec< -c(inPvec,inP)#连接每次迭代的结果
}

inP
#NULL

myFunction(tablist [[1]])
#[1] 123410

编辑为可行示例:apol



如果您在示例中运行循环,则inP返回NULL,就像在Pvec中一样。在函数中运行单个表返回正确的值。然而,inP在调用它时是NULL,所以我猜这是我的问题所在。



我希望循环遍历函数的所有内容都返回到单个函数例如:

  inPVec 
#[1]123410 123410123410等

非常感谢任何帮助。

解决方案

问题是您没有得到你的函数的结果:

  inPvec< -c()#predefine将结果连接到
中的向量(x in {#tabs将多个表保存为列表
inPvec< -c(inPvec,myFunction(x))#concatenate每次迭代的结果
}

并且您应该更正您的功能:

  myFunction< ;(函数(制表符){​​#为表达式输入
inP < - c()
for(x in 1:dim(tab)[1]){#iterate over rows
rname< ; -rownames(tab)[x] #rowname如果(rname ==something)inP< -c(inP,rname)#trivial work
}
return(inP)#返回在$ b $上创建的向量b}


I have stayed away from functions in R but decided it is better practice. Now I have this issue. I write my function:

 myFunction<-function(tab){ #takes tabular input
     inP<-c()
     for (x in 1:dim(tab)[1]){ #iterate over rows
         rname<-rownames(tab)[x] #rownames as output
         if(rname<5) inP<-c(inP,rname) #trivial work
     }
     return(inP) #return the vector made above
 }

 tablist<-as.list(paste("tab",1:4,sep=""))
 for (x in 1:length(tablist)){
     tablist[[x]]<-table(c(1:10),c(1:10))
 }

 inPvec<-c() #predefine vector to concatenate results into
 for (x in 1:length(tablist)){ #tabs holds multiple tables as a list
      myFunction(tablist[[x]]) #run myFunction for each table held in tabs
      inPvec<-c(inPvec,inP) #concatenate results from each iteration
 }

 inP
 #NULL

 myFunction(tablist[[1]])
 #[1] "1" "2" "3" "4" "10"

Edited as workable example: apologies for laziness.

If you run for the loop in the example, inP returns NULL, as does inPvec. Bu running single tables in the function return the correct value. However, inP is NULL on calling it, so I guess this is where my issue is.

I want everything from the loop iterating over the function to be returned to a single vector eg:

 inPVec
 #[1] "1" "2" "3" "4" "10" "1" "2" "3" "4" "10" "1" "2" "3" "4" "10" etc

Any help much appreciated.

解决方案

The problem is that you do not get the result of your function:

inPvec<-c() #predefine vector to concatenate results into
for (x in tabs){ #tabs holds multiple tables as a list
     inPvec<-c(inPvec,myFunction(x)) #concatenate results from each iteration
}

and you should correct your function too:

myFunction<-function(tab){ #takes tabular input
    inP <- c()
    for (x in 1:dim(tab)[1]){ #iterate over rows
        rname<-rownames(tab)[x] #rownames as output
        if(rname=="something") inP<-c(inP,rname) #trivial work
    }
return(inP) #return the vector made above
}

这篇关于在循环R中迭代时从函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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