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='')

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)

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 ?


  • $\begingroup$ Welcome to the site. Have you tried to do a loop for each column to correlate them with the Proteome_size? $\endgroup$
    – llrs
    Nov 8, 2018 at 16:01
  • $\begingroup$ 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 $\endgroup$ Nov 8, 2018 at 21:33
  • $\begingroup$ 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! $\endgroup$
    – gabt
    Nov 9, 2018 at 9:04
  • 1
    $\begingroup$ 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. $\endgroup$
    – haci
    Nov 12, 2018 at 18:06

2 Answers 2


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


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.


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 $


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.