Reputation: 1013
I need to pick out individual observations from a density object in R. This is related to a prior post except my data is formatted differently.
I begin with a matrix of gene expression values "b", where the columns represent 10 observations and rows represent expression levels for 1000 genes.
b = matrix(rexp(1000),ncol=10)
Next, I plot the densities with each observation as a separate line.
d = apply(b, 2, density, na.rm = TRUE)
plot(b, type="n", ylim=c(0,1))
for (i in 1:10) { lines( d[[i]] ) }
abline(h=0.5)
How do I pick out the columns in 'b' whose densities are below the horizontal line at 'b[,1]=0.5'?
Upvotes: 0
Views: 141
Reputation: 206606
If you want to get the estimated density at a particular value, you can use approx
along with the points from the density object. For example
yv <- sapply(d, function(d) {
approx(d$x, d$y, .5)[["y"]]
})
yv
# [1] 0.7158584 0.5516512 0.7049119 0.6180026 ...
This will predict the xvalue at x=.5 for each of the densities 10. To see which ones are below the horizontal line, you can do
which(yv<.5)
# [1] 5 # (your results may differ due to the random input)
So we see only the 5th column meets the criteria.
Upvotes: 1