Return value from function when iterating in a loop R

25,723

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
}
Share:
25,723

Related videos on Youtube

bruce01
Author by

bruce01

Biologist, so excuse the [poor programming=P

Updated on August 22, 2020

Comments

  • bruce01
    bruce01 over 3 years

    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.

    • Paul Hiemstra
      Paul Hiemstra over 10 years
      Please help us help you by providing us with a reproducible example (i.e. code and example data), see stackoverflow.com/questions/5963269/… for details.
    • bruce01
      bruce01 over 10 years
      Sorry, edited now to run as standalone example.
  • bruce01
    bruce01 over 10 years
    Yes, sorry again, inP was meant to be defined inside the function.
  • bruce01
    bruce01 over 10 years
    Great, thanks Karl, I understand: the return value is myFunction(x), as opposed to the value defined and returned in myFunction (eg inP).
  • WestCoastProjects
    WestCoastProjects almost 6 years
    what an "interesting" / different syntax R uses for concatenation. Glad to have stumbled onto this answer.