title: R function does not return anticipated vector
body: <p>I am trying to calculate the mean (as well as other calculations) based on two groupings (phylum and environment) and I would like to redirect to output to a file. I know that the below code works.</p>
  
  <pre><code>new_df = myDF[(myDF$Environment=='Water_MarineTreated') &amp; (myDF$Phylum=='Acidobacteria'),]
  print(mean(new_df$pH))
  </code></pre>
  
  <p>However, because there are so many environments and numerous phyla, I feel that a function containing loops is the best way to go. I have a function that takes the name of a vector and the name of the calculation (e.g. mean, sd, var, etc...) loops through each environment and each phylum, calculates the mean pH for each permutation, adds it to a vector, and the returns the vector. Unfortunately, the return value is "numeric(0)". While this is returning what I tell it to, it is not what I want.  </p>
  
  <p>I think the rule is one question per post so if someone can explain why there is an empty vector that is return rather than a vector filled with the pH means, I would appreciate it. And if the rule can be bent a bit and someone can answer why "eName = numeric()" does not work, I would appreciate that too. If I place a dummy print statement inside the inner loop, when I use eName = numeric(), nothing is printed where as if I initialize Water_MarineTreated = numeric(), the dummy statement in printed.</p>
  
  <p>My function and the function call can be seen below.</p>
  
  <pre><code>    fileName = 'mini.txt'    
      myDF = read.csv(fileName, header = TRUE, sep = ' ')
      environment = unique(unlist(myDF$Environment, use.names = FALSE))
      phyla = unique(unlist(myDF$Phylum, use.names = FALSE))
  
      Statistics = function(eName, funName)
      {
         #eName = numeric() #This approach does not work?!!
         for (i in environment)
         {   
            for (j in phyla)
            {   
               stats_df = myDF[(myDF$Environment==i) &amp; (myDF$Phylum==j),]
  
               if (i == deparse(substitute(eName)))
               {   
                  #Water_MarineTreated == c(Water_MarineTreated, funName(as.numeric(stats_df$pH)))
                  eName == c(eName, funName(as.numeric(stats_df$pH)))
                  print('dummy_statement')
                }   
            }   
         }   
         return(eName)
      }
  
      Water_MarineTreated = numeric()
      Water_MarineTreated = Statistics(Water_MarineTreated, mean)
      print(Water_MarineTreated)
  </code></pre>
  
  <p>A sample of the input looks like this:</p>
  
  <pre><code>Phylum pH Environment 
  Acidobacteria 5.4 Water_MarineTreated 
  Acidobacteria 6.1 Water_PondTreated
  Acidobacteria 6.1 Water_MarineTreated 
  Acidobacteria 5.6 Water_MarineTreated 
  Acidobacteria 6.2 Water_MarineTreated 
  Deinococcus_Thermus 4.9 Water_MarineTreated 
  Firmicutes 5.1 Water_MarineTreated 
  Firmicutes 5.5 Water_MarineTreated 
  </code></pre>