<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>

CC BY-SA 4.0
2018-11-12T18:04:24.063Z
2018-11-12T18:04:24.063Z
haci
<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>

CC BY-SA 4.0
2018-11-14T11:36:28.890Z
2018-11-14T11:36:28.890Z
Ashley Xavier
<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>

CC BY-SA 4.0
2018-11-08T16:01:28.893Z
Welcome to the site. Have you tried to do a loop for each column to correlate them with the Proteome_size?
llrs
CC BY-SA 4.0
2018-11-08T21:33:45.063Z
Thanks for your quick response. I am a beginner  in R.  To do that  need to create a vector before  as : gene.names <- colnames(data[, "proteome_size"]). unfortunately it failed and the  vector is NULL with length 0.  I am not so good in for loop too
Dieunel Derilus
CC BY-SA 4.0
2018-11-09T09:04:33.193Z
Sorry, I don't actually understand what the variable **awe** is doing. Also I don't see which variable will change through the loop. It's quite easy to write a for loop in R if you can tell which variable needs to be investigated!
gbt
CC BY-SA 4.0
2018-11-12T18:06:54.090Z
Your code `gene.names <- colnames(data[, "proteome_size"])` produces a NULL vector since R is case sensitive and your data frame does not have a "proteome_size" column.
haci
<r><correlation>
Multiple correlation with R
# Multiple correlation with R

(Asked by: username_0 on 2018-11-08)

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

**Tags:** r, correlation
> **Comments:**
> **username_1**: Welcome to the site. Have you tried to do a loop for each column to correlate them with the Proteome_size?  
> **username_0**: Thanks for your quick response. I am a beginner  in R.  To do that  need to create a vector before  as : gene.names <- colnames(data[, "proteome_size"]). unfortunately it failed and the  vector is NULL with length 0.  I am not so good in for loop too  
> **username_2**: Sorry, I don't actually understand what the variable **awe** is doing. Also I don't see which variable will change through the loop. It's quite easy to write a for loop in R if you can tell which variable needs to be investigated!  
> **username_3**: Your code `gene.names <- colnames(data[, "proteome_size"])` produces a NULL vector since R is case sensitive and your data frame does not have a "proteome_size" column.  

---

## 2 answers

### Answer by username_3 on 2018-11-12. Score: 1

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).


---

### Answer by username_4 on 2018-11-14. Score: 1

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 $


---

