title: R - Automating analysis over several variables by groups (Chi square)
body: <pre><code>RING    SPECIES        SEX AGE  FAT WEIGHT  WING    WINGPRI BEAK    TARSUS   
  H8309   ACCIPITER NISUS M   5   0   141     199     117     19,2    52      
  K617    ACCIPITER NISUS F   4   0   288,5   232     167     20,4    62,2    
  A264905 ACROCEPHALUS    F   4   2   11,8    64,5    NA      NA      NA      
  A358705 ACROCEPHALUS    M   3   2   11      66      50      18,2    22      
  A432721 ACROCEPHALUS    U   4   6   14,5    63      48      16      21,9    
  O59461  AEGITHALOS      M   4   0   6,4     57      42      8,2     13,8    
  O92094  AEGITHALOS      F   2   0   6,8     56      38      7,96    16,54   
  O92095  AEGITHALOS      U   2   0   7       58      44      8,78    17,85   
  </code></pre>
  
  <p>This is a small sample of my data frame ("amostra", at the moment I'm trying to figure out if there are any differences between sex in each individual species (I have over 60 in the original df), and for that I was told the best aproach is to use Chi-square values of the variables WEIGHT, WING, WINGPRI, BEAK and TARSUS</p>
  
  <p>So I need to apply a chi-square test for each individual species, to determine if there are any differences between sexes using all these 5 variables independently </p>
  
  <p>I have been strugling with this for quite a few days and so far the best I could do is this:</p>
  
  <pre><code>for(i in unique(amostra$SPECIES)){
    for (j in 6:10){
    print(
      colnames(amostra[j]))
      names(amostra$SPECIES)
      print(
      chisq.test(amostra$SEX, amostra[,j]))}
  }
  </code></pre>
  
  <p>Which gives me the correct output for every 5 variables but multiplied by the number of unique species I have, so I get the same p-value for TARSUS x60 and not the unique p-values for each species
  Ex. just from the variable TARSUS:</p>
  
  <pre><code>[1] "TARSUS"
  
      Pearson's Chi-squared test
  
  data:  amostra$SEX and amostra[, j]
  X-squared = 1072, df = 758, p-value = 3.53e-13
  </code></pre>
  
  <p>I also tried this:</p>
  
  <pre><code>subset1 &lt;- amostra[, c(2,3,6:10)]
  
  subset1$SPECIES&lt;- as.factor(subset1$SPECIES)
  
  analise&lt;- function(subset1){
    for (i in 3:7){
      print(
        colnames(amostra[i]))
      print(
        chisq.test(amostra[,2],amostra[,i]))
    }
    subset1
  }
  
  by(subset1,subset1$SPECIES,FUN = analise)
  </code></pre>
  
  <p>Which gives me a huge output that I cant see in its entirety, but the initial output is the same as the above but instead of now having the result of the chi-square test grouped by species I get this for all my species...</p>
  
  <pre><code>---------------------------------------------------------------------------------------- 
  subset1$SPECIES: PASSER SP.
          SPECIES SEX WEIGHT WING WINGPRI  BEAK TARSUS
  1522 PASSER SP.   F   25.5   74      55 14.64  21.51
  1523 PASSER SP.   F     NA   76      56    NA     NA
  1524 PASSER SP.   F   29.5   78      58 14.70  20.40
  ----------------------------------------------------------------------------------------
  </code></pre>
  
  <p>I hope I could make my problem clear, this is my first post so I'm sorry for any mistakes</p>
  
  <p>Thank you in advance</p>