title: Repeating a function over many variables
body: <p>I need to repeat a function over 43 variables. More specifically, I am running a non-parametric repeated measures test on my data. This should result in two final data sets - one data set with the overall test stats for each variable and a second which contains specific tests for each pair of times to show where the differences are.</p>
  
  <p>I have created my list of variables for looping and the function but am unsure how to progress to make the function run over all variables. </p>
  
  <p>I would be most grateful for any help with:
  1. Running my function over each variable
  2. Combining the the results for each variable into two data frames (one for overall tests and the second for specific time differences as highlighted in the code).</p>
  
  <p>Many thanks in advance!</p>
  
  <p>Here is my data:</p>
  
  <pre><code>&gt; dput(junk)
  structure(list(Patient = c("TLA001", "TLA001", "TLA001", "TLA001", 
  "TLA001", "TLA001", "TLA002", "TLA002", "TLA002", "TLA002", "TLA002", 
  "TLA003", "TLA003", "TLA003", "TLA003", "TLA003", "TLA003", "TLA004", 
  "TLA004", "TLA004", "TLA004", "TLA004", "TLA004", "TLA005", "TLA005", 
  "TLA005", "TLA005", "TLA005", "TLA005", "TLA006", "TLA006", "TLA006", 
  "TLA006", "TLA006", "TLA006", "TLA006", "TLA008", "TLA008", "TLA008", 
  "TLA008", "TLA008", "TLA008", "TLA009", "TLA009", "TLA009", "TLA009", 
  "TLA009", "TLA009", "TLA010", "TLA010", "TLA010", "TLA010", "TLA010", 
  "TLA010", "TLA011", "TLA011", "TLA011", "TLA011", "TLA011", "TLA011", 
  "TLA012", "TLA012", "TLA012", "TLA012", "TLA012", "TLA012", "TLA012", 
  "TLA013", "TLA013", "TLA013", "TLA013", "TLA013", "TLA013", "TLA013", 
  "TLA014", "TLA014", "TLA014", "TLA014", "TLA015", "TLA015", "TLA015", 
  "TLA015", "TLA015", "TLA015"), Night = c("TLA -3", "TLA -2", 
  "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", 
  "TLA 1", "TLA 2", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", 
  "TLA 3", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", 
  "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", 
  "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA 4", "TLA -3", 
  "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", 
  "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", 
  "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", "TLA 1", 
  "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", 
  "TLA 3", "TLA 4", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", 
  "TLA 3", "TLA 4", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA -3", 
  "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3"), var = c(0.1, 
  0, 0, 0, 0, 0, 0.5, 0, 0, 2.8, 0.2, 0.3, 0, 0.4, 0.3, 0, 2.2, 
  1.1, 0.6, 2.1, 1.4, 1.7, 0.9, 0.3, 0.3, 0, 1, 0, 0.3, 0, 0, 0, 
  0, 0, 0, 0, 0.1, 0.2, 0.1, 0.1, 0.4, 0, 0.4, 0.1, 0, 0, 0, 0, 
  1.1, 0.8, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 0, 2, 0.9, 0.2, 0.4, 0.3, 
  1, 0.9, 0, 0, 0, 0.3, 0.3, 0, 0, 1.2, 0.9, 0.6, 0.7, 0.7, 0, 
  0, 0, 0, 0), SF_Cluster = c("Responder", "Responder", "Responder", 
  "Responder", "Responder", "Responder", "Non-responder", "Non-responder", 
  "Non-responder", "Non-responder", "Non-responder", "Responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Responder", "Non-responder", "Non-responder", "Non-responder", 
  "Non-responder", "Non-responder", "Non-responder", "Non-responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Non-responder", "Non-responder", "Non-responder", 
  "Non-responder", "Non-responder", "Non-responder", "Responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Non-responder", "Non-responder", "Non-responder", 
  "Non-responder", "Non-responder", "Non-responder", "Non-responder", 
  "Responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Responder", "Non-responder", "Non-responder", "Non-responder", 
  "Non-responder", "Responder", "Responder", "Responder", "Responder", 
  "Responder", "Responder")), row.names = c(NA, -84L), class = c("tbl_df", 
  "tbl", "data.frame"), spec = structure(list(cols = list(Patient = structure(list(), class = c("collector_character", 
  "collector")), Night = structure(list(), class = c("collector_character", 
  "collector")), var = structure(list(), class = c("collector_double", 
  "collector")), SF_Cluster = structure(list(), class = c("collector_character", 
  "collector"))), default = structure(list(), class = c("collector_guess", 
  "collector"))), class = "col_spec"))
  </code></pre>
  
  <p>Here is my code: </p>
  
  <pre><code>names(junk)
  varlist=names(junk)[5:9]
  
  library("nparLD")
  
  nonparLD=function(var, data=junk) {
    boxplot(var  ~ Night, data = data, lwd = 2, xlab = "time", ylab="N",
          font.lab = 2, cex.lab = 2, main = "Box Plots: var  ")
  
  ex.f1np &lt;- nparLD(var  ~ T, data = data, subject = "id",
                    description = FALSE)
  plot(ex.f1np,main = "Relative Effects: var  " )
  summary(ex.f1np)
  
  #Data frame 1: Overall test result
  r_var=rbind(ex.f1np$ANOVA.test,ex.f1np$Wald.test )
  rownames(r_var)=c("varAT","var_WT")
  
  
  #Data frame 2: which time differences are significant?
  
  m1_2 &lt;- which(((data$T == 1) + (data$T == 2)) == 1)
  m1_3 &lt;- which(((data$T == 1) + (data$T == 3)) == 1)
  m1_4&lt;- which(((data$T == 1) + (data$T == 4)) == 1)
  m1_5&lt;- which(((data$T == 1) + (data$T == 5)) == 1)
  m1_6&lt;- which(((data$T == 1) + (data$T == 6)) == 1)
  
  
  ex.f1np1_2 &lt;- nparLD(var ~ T, data = data[m1_2,], subject = "id",
                       description = FALSE)
  
  ex.f1np1_3 &lt;- nparLD(var ~ T, data = data[m1_3,], subject = "id",
                       description = FALSE)
  
  ex.f1np1_4 &lt;- nparLD(var ~ T, data = data[m1_4,], subject = "id",
                       description = FALSE)
  
  ex.f1np1_5 &lt;- nparLD(var ~ T, data = data[m1_5,], subject = "id",
                       description = FALSE)
  
  ex.f1np1_2 &lt;- nparLD(var ~ T, data = data[m1_2,], subject = "id",
                       description = FALSE)
  
  ex.f1np1_6 &lt;- nparLD(var ~ T, data = data[m1_6,], subject = "id",
                       description = FALSE)
  
  
  summary(ex.f1np1_2)
  summary(ex.f1np1_3)
  summary(ex.f1np1_4)
  summary(ex.f1np1_5)
  summary(ex.f1np1_6)
  
  
  #Data set for test stats
  r_varT=rbind(ex.f1np1_2$ANOVA.test,ex.f1np1_2$Wald.test,
                        ex.f1np1_3$ANOVA.test,ex.f1np1_3$Wald.test,
                        ex.f1np1_4$ANOVA.test,ex.f1np1_4$Wald.test,
                        ex.f1np1_5$ANOVA.test,ex.f1np1_5$Wald.test,
                        ex.f1np1_6$ANOVA.test,ex.f1np1_6$Wald.test)
  
  #Data set for time differences for each var
  r_varT=t(r_varT)
  colnames(r_varT)=c("AT12","WT12","AT13","WT13","AT14","WT14","AT15","WT15","AT16","WT16" )
  rownames(r_varT)=c("varS", "varDF","varP")
  }
  </code></pre>