My data frame (g) contains 2 columns with continues data and other columns with categorical data. I want to test for correlations between the 2 continues variables, in diffe
I think you want this inside of function r
:
if ((length(x[[1]]))>2)
cor.test(x[[1]],x[[2]],use="pairwise.complete.obs")[3:4] else NA
x[[1]]
is a vector, whereas x[[1]][1]
is a single element of that vector. You clearly want vectors for cor.test
and not single elements.
In addition, a vector has a length
but nrow
is not appropriate.
The error that you get is a result of nrow(x[[1]][1])
evaluating to NULL
, so nrow(x[[1]][1]) > 2
evaluates to logical(0)
. The argument to if
should be a logical
of length 1.
If I understand right, you want to compute the correlations between GDW and GN for every value in the column M1 (that is, by splitting at every unique value of M1).
Using Hadley's plyr
require(plyr)
# assuming the data.frame is df (Note: factor M1 if necessary)
daply(df, .(M1), function(y) cor(y$GDW, y$GN))