[R] apply pairs function to multiple columns in a data frame

jarvisma marleyjarvis at gmail.com
Fri Feb 10 21:13:02 CET 2012

I am very new to R and programming and thank you in advance for your patience
and help with a complete novice!

I am working with a large multivariate data set that has 10 explanatory
environmental variables (e.g. temp, depth) and over 60 response variables
(each is a separate species).  My data frame is set up like the simplified
version below:

JulianDay  Temperature  Salinity  Depth  Copepod  Barnacle  Gastropod 
222              12.1                    33            0.3         500           
756             0                     178
222              12.3                    33.2         1.1        145            
111             0                     0
223              11.1                    33.1         7            752           
234             12                   0

Where JulianDay, Temperature, Salinity, Depth, Copepod, Barnacle, Gastropod,
and Bivalve are the column headers.

I am using the pairs function in R to explore my data.  My data frame is
named Sunset.
Using this code:

Z=cbind(Sunset$Copepod,Sunset[,c(1:10)])   #the first 10 columns of my data
frame are explanatory variables such as temp
Pairs.Copepod=pairs(Z,main="Copepods vs. explanatory variables",

I get a great pair plot of Copepods vs. all of the 10 explanatory
environmental variables.  I would like to do this for each of my 60+
species.  I can't just make one big pair plot of all of my explanatory
variables vs. all of my species because I have too many.  So instead I would
like a separate pair plot for each species (each column of data after the
first 10 columns of explanatory variables)

I would like to be able to write a loop that creates all of these plots at
once, but haven't been able to do so.  Ideally, each pair plot would have
the main title be the column header (e.g. Copepod) for that plot.  I would
love to include a way to save each pair plot as  separate jpeg to a folder
on my desktop with a file name that includes the species name (e.g.

Again, I am very new to R and to programing so I would GREATLY appreciate
anyone patient and kind enough to respond with lots of detail so I can
hopefully follow your answer and suggestions.  I have searched but haven't
been able to find a way to write a loop that uses each column of data, so I
would love some help!

Thank you!

View this message in context: http://r.789695.n4.nabble.com/apply-pairs-function-to-multiple-columns-in-a-data-frame-tp4377425p4377425.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list