Skip to content
Prev 277481 / 398506 Next

correlations between columns for each row

Error in cor(x[a], x[b], use = "complete.obs") : 'x' must be numeric

This is strange, it works on your example (and you've understood what I'm 
trying to do perfectly), but then when I use it on the original data it 
comes up with the error above
I've checked str() and the columns are all numeric

???

-----Original Message----- 
From: Joshua Wiley
Sent: Tuesday, November 15, 2011 7:14 PM
To: robgriffin247
Cc: r-help at r-project.org
Subject: Re: [R] correlations between columns for each row

Hi Rob,

Here is one approach:


## define a function that does the calculations
## (the covariance of two vectors divided by the square root of
## the products of their variances is just a correlation)
rF <- function(x, a, b) cor(x[a], x[b], use = "complete.obs")

set.seed(1)
bigdata <- matrix(rnorm(271 * 13890), ncol = 271)

results <- apply(bigdata, 1, FUN = rF, a = 174:213, b = 214:253)

## combine
bigdata <- cbind(bigdata, iecorr = results)

Hope this helps,

Josh

On Tue, Nov 15, 2011 at 8:42 AM, robgriffin247
<robgriffin247 at hotmail.com> wrote: