Re: [R] cor() btwn columns in two matrices - no complete element pairs

0 views
Skip to first unread message

rcoder

unread,
Aug 15, 2008, 10:13:24 AM8/15/08
to r-h...@r-project.org

Is there any way to increment the loop when the error occurs, returning an NA
in the results matrix. I have included an if starement to only calculate
cor() when columns are !all(is.na()), but I still exit out with this error,
albeit after some loops through the matrix - i.e. when the condition is
satisfied, but the data rows do not overlap at all.

rcoder wrote:
>
> Hi everyone,
>
> I'm trying to calculate correlation coefficients between corresponding
> columns in two matrices with identical dimensions but different data. The
> problem is that the matrices contain NAs in different locations. I am
> using the following code to try to calculate correlations between complete
> sets of data:
>
> #Code start
> maxcol<-ncol(mat1)
> for (i in 1:maxcol)
> {
> corr_results[1,i]<-cor(mat1[,i],mat2[,i], use="complete.obs")
> }
> #Code end
>
> ...but I get the following error message:
>
> Error in cor(mat1[,i], mat2[,i], use="complete.obs") :
> no complete element pairs
>
> Is there something I'm not including in the 'cor' parentheses? I apologise
> for not including the true original data frames.
>
> Thanks,
>
> rcoder
>

--
View this message in context: http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-element-pairs-tp18998875p18999356.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@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.

rcoder

unread,
Aug 15, 2008, 9:44:36 AM8/15/08
to r-h...@r-project.org

Hi everyone,

I'm trying to calculate correlation coefficients between corresponding
columns in two matrices with identical dimensions but different data. The
problem is that the matrices contain NAs in different locations. I am using
the following code to try to calculate correlations between complete sets of
data:

#Code start
maxcol<-ncol(mat1)
for (i in 1:maxcol)
{
corr_results[1,i]<-cor(mat1[,i],mat2[,i], use="complete.obs")
}
#Code end

...but I get the following error message:

Error in cor(mat1[,i], mat2[,i], use="complete.obs") :
no complete element pairs

Is there something I'm not including in the 'cor' parentheses? I apologise
for not including the true original data frames.

Thanks,

rcoder
--
View this message in context: http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-element-pairs-tp18998875p18998875.html

Daniel Malter

unread,
Aug 16, 2008, 2:02:02 PM8/16/08
to rcoder, r-h...@r-project.org
Hi,

the argument use should be: use="p" (for pairwise complete obs.)

Daniel


-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-...@r-project.org [mailto:r-help-...@r-project.org] Im
Auftrag von rcoder
Gesendet: Friday, August 15, 2008 9:45 AM
An: r-h...@r-project.org
Betreff: [R] cor() btwn columns in two matrices - no complete element pairs

rcoder

unread,
Aug 16, 2008, 3:41:39 PM8/16/08
to r-h...@r-project.org

Hi Daniel,

Thanks for your reply. I experimented with various options since my poet and
eventually tried use="pairwise.complete.obs" which seemed to do the trick.

Thanks,

rcoder

--
View this message in context: http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-element-pairs-tp18998875p19014384.html

Reply all
Reply to author
Forward
0 new messages