text: user3646: I have a data frame presenting different variables in column and for different species name in rows as follow. In the column a have only one independent variables(Proteome size) and all the others are independent variables. I want to use the PIC package to correlate all these dependent variables separately to my independent variables, a way to generate the R coefficient and p-value.
  data <- read.table("t1.txt", row.names = 1, header = T,sep='')
  
  data 
  path1 path2 path3 path4 Proteome_size
  ahli 4.0 -3.2 0.4 -1.0 -0.2
  alayoni 3.8 3.4 -1.8 2.2 -0.3
  alfaroi 3.5 2.6 0.8 -2.4 -0.5
  aliniger 4.0 0.1 -1.7 2.4 0.0
  allisoni 4.4 2.0 -3.7 0.5 -0.1
  allogus 4.0 -2.8 0.6 -1.0 0.6
  altitudinalis 3.8 2.9 -6.1 2.3 1.2
  alumina 3.6 0.7 1.5 -2.7 -1.3
  alutaceus 3.6 1.2 -0.6 -1.7 0.3
  angusticeps 3.8 4.6 -2.0 1.2 1.7
  
  I have this code following code that can correlate any one of the ‘path*’ column to proteome size:
  data <- read.csv("data.csv", row.names = 1
  tree <- read.tree("anolis.phy")
  Proteome <- data[, "Proteome_size"]
  awe <- data[, "path1"]
  names(Proteome_size) <- names(var1) <- rownames(data)
  hPic <- pic(proteome_size, tree)
  aPic <- pic(gene1, tree)
  picModel <- lm(hPic ~ aPic - 1)
  summary(picModel)
  
  My question is that, how can I implement this code to do that for all the columns simultaneously and store the statistics info as (R and p_value) for each variable somewhere ?
  Thanks
  
  user3541: One option is to use one of the apply family functions. If you enclose your code above (which generates errors by the way) in a function, you can then "apply" this function on the rows or columns of your data with something like apply(X, MARGIN, FUN, …), the margin being 1 for rows or 2 for columns (as in your case).
  
  user3680: Like haci mentioned, you can use the apply family of functions to loop over all your columns.
  apply(data,2, function(path_name){ summary(lm(data$Proteome_size ~ path_name -1))$adj.r.squared })    
  
  this gives you the adjusted r square value for your linear model, and 
  apply(data,2, function(path_name){ summary(lm(data$Proteome_size ~ path_name -1))$coefficients[,4] })    
  
  would give you the p-values.
  What happens here is apply takes your matrix and chops it up into pieces(a piece is a column here because I gave the option 2) and each pieces is fed into the function as 'path_name' and the output for all the pieces is given back to you. 
  You might get a warning about getting a perfect fit because I am correlating 'Proteome_size' against itself. 
  Also, 
  awe <- data[, "path1"]
  awe <- data$path1
  
  are the same when dealing with columns and the second can be more convenient because you can search for autocomplete by hitting tab after typing the $
  
original_text: 3646: <p>I have a data frame presenting different variables in column and for different species name in rows as follow. In the column a have only one independent variables(Proteome size) and all the others are independent variables. I want to use the PIC package to correlate all these dependent variables separately to my independent variables, a way to generate the R coefficient and p-value.</p>
  
  <pre><code>data &lt;- read.table("t1.txt", row.names = 1, header = T,sep='')
  
  data 
  path1 path2 path3 path4 Proteome_size
  ahli 4.0 -3.2 0.4 -1.0 -0.2
  alayoni 3.8 3.4 -1.8 2.2 -0.3
  alfaroi 3.5 2.6 0.8 -2.4 -0.5
  aliniger 4.0 0.1 -1.7 2.4 0.0
  allisoni 4.4 2.0 -3.7 0.5 -0.1
  allogus 4.0 -2.8 0.6 -1.0 0.6
  altitudinalis 3.8 2.9 -6.1 2.3 1.2
  alumina 3.6 0.7 1.5 -2.7 -1.3
  alutaceus 3.6 1.2 -0.6 -1.7 0.3
  angusticeps 3.8 4.6 -2.0 1.2 1.7
  </code></pre>
  
  <p>I have this code following code that can correlate any one of the ‘path*’ column to proteome size:</p>
  
  <pre><code>data &lt;- read.csv("data.csv", row.names = 1
  tree &lt;- read.tree("anolis.phy")
  Proteome &lt;- data[, "Proteome_size"]
  awe &lt;- data[, "path1"]
  names(Proteome_size) &lt;- names(var1) &lt;- rownames(data)
  hPic &lt;- pic(proteome_size, tree)
  aPic &lt;- pic(gene1, tree)
  picModel &lt;- lm(hPic ~ aPic - 1)
  summary(picModel)
  </code></pre>
  
  <p>My question is that, how can I implement this code to do that for all the columns simultaneously and store the statistics info as (R and p_value) for each variable somewhere ?</p>
  
  <p>Thanks</p>
   3541: <p>One option is to use one of the <code>apply</code> family functions. If you enclose your code above (which generates errors by the way) in a function, you can then "apply" this function on the rows or columns of your data with something like <code>apply(X, MARGIN, FUN, …)</code>, the margin being 1 for rows or 2 for columns (as in your case).</p>
   3680: <p>Like haci mentioned, you can use the apply family of functions to loop over all your columns.</p>
  
  <pre><code>apply(data,2, function(path_name){ summary(lm(data<span class="math-container">$Proteome_size ~ path_name -1))$</span>adj.r.squared })    
  </code></pre>
  
  <p>this gives you the adjusted r square value for your linear model, and </p>
  
  <pre><code>apply(data,2, function(path_name){ summary(lm(data<span class="math-container">$Proteome_size ~ path_name -1))$</span>coefficients[,4] })    
  </code></pre>
  
  <p>would give you the p-values.</p>
  
  <p>What happens here is apply takes your matrix and chops it up into pieces(a piece is a column here because I gave the option 2) and each pieces is fed into the function as 'path_name' and the output for all the pieces is given back to you. </p>
  
  <p>You might get a warning about getting a perfect fit because I am correlating 'Proteome_size' against itself. </p>
  
  <p>Also, </p>
  
  <pre><code>awe &lt;- data[, "path1"]
  awe &lt;- data$path1
  </code></pre>
  
  <p>are the same when dealing with columns and the second can be more convenient because you can search for autocomplete by hitting tab after typing the $</p>
  