Reputation: 1057
I want to calculate the correlation between two datasets with a condition from another data.I want the top 50% of P, this corresponds to the values of P greater than the median.
P=c(1,6,5,6,2,8,5)
sf=c(1,2,6,6,4,5,5)
Pf=c(1,6,5,8,4,8,5)
normal corr:
cor(sf,Pf)
with condition:
cor(sf[P > median(P)], Pf[P > median(P)])
this worked perfectly.How Can I apply the same thing with my real data?
with condition(using dir3(p is dir3 here)):
???
Thanks in advance
Upvotes: 0
Views: 234
Reputation: 32361
Exactly the same code should work:
function(x){cor(x[,1],x[,2])}
can be written as
function(x) {
P <- x[,3]
sf <- x[,1]
Pf <- x[,2]
cor(sf, Pf)
}
which becomes
function(x) {
P <- x[,3]
sf <- x[,1]
Pf <- x[,2]
i <- P > median(P)
cor(sf[i], Pf[i])
}
Upvotes: 1