Alex,

Here's one way to do it, using for() loops.

Jean


library(Hmisc)
# using Swiss Fertility and Socioeconomic Indicators (1888) Data
m <- data.matrix(swiss)
output <- rcorr(m)

varnames <- dimnames(m)[[2]]
nvar <- length(varnames)
# for loops through all possible pairs
for(i in 1:(nvar-1)) {
for(j in (i+1):nvar) {
# get short variables to ease reading
r <- output$r[i, j]
n <- output$n[i, j]
 P <- output$P[i, j]
if(n >= 5 & (r <= -0.5 | r >= 0.5)) {
png(paste0(varnames[i], "_vs_", varnames[j], ".png"))
 plot(m[, i], m[, j], xlab=varnames[i], ylab=varnames[j],
main=paste0("R = ", format(round(r, 2), nsmall=2), ", P = ",
format(round(P, 2), nsmall=4)))
 abline(lsfit(m[, i], m[, j]))
dev.off()
}
 }}



On Mon, Dec 30, 2013 at 10:18 AM, Alexander Schuster <ga...@gmx.net> wrote:

> Hi,
>
> i have used rcorr() for calculating pearsons r and according p-values
> for my data, giving me 2 matrices.
>
> Now I would like to print scatterplots for all results with "good"
> correlation values.
>
> So i need a way to extract the row-name and column-name for each item in
> the matrix with "good" r-values, so i can use them in the
> plot()-function on my original dataframe programmatically.
>
> below is what i have got - and now i'm stuck:
>
> --------------------------------------
> incomingData <- read.csv(inputfile, header=TRUE, na="NA")
> datamatrix <- data.matrix(incomingData)
> library(Hmisc)
> output <- rcorr(datamatrix, type="pearson")
>
> # get short variables to ease reading
> r <- output$r
> n <- output$n
> P <- output$P
>
> r[n<5]<-NA # ignore less than five observations
> r[r>-0.5 & r<0.5]<-NA # take only "good" korrelations
> P[is.na(r)]<-NA # delete P values for deleted korrelations
>
> r <- format(round(cbind(rep(-1.11, ncol(r)), r), 2))[,-1] ## trunctuate
> matrix with correlations to 2 decimals
>
> P <- format(round(cbind(rep(-1.11, ncol(P)), P), 4))[,-1] ## trunctuate
> matrix with P-Values to 4 decimals
>
> make_plot <- function(a,b,Rval,Pval,aname,bname) {
>         png(paste(aname,'_vs_',bname,'.png', sep=""))
>         plot(a,b, main="Rval(p=Pval)")
>         fitline <- lm(a~b)
>         abline(fitline)
>         dev.off()
> }
>
> -------------------------------------
>
> Big Thanks for any ideas on this, Alex
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to