Adrian Adrian - 2 months ago 8
R Question

R: finding indices of columns in a data.frame

m <- lm(Sepal.Length ~ Sepal.Width + Petal.Width, data = iris)
beta = dfbetas(m)
> head(beta)
(Intercept) Sepal.Width Petal.Width
1 -0.0018633253 0.0054762565 -0.0096031648
2 0.0094916858 -0.0062007468 -0.0137816086
3 -0.0221770886 0.0069280848 0.0540485812
4 -0.0408776612 0.0219247324 0.0731671391
5 0.0071436202 -0.0134636336 0.0150509697
6 0.0006264958 -0.0007979264 0.0001755277


apply(data.frame(beta), 2, function(x) which(abs(x) < 0.1632993))


I have a matrix named
beta
that contains 3 columns. After converting it to type
data.frame
, I wanted to find the indices using the
which
function of observations whose absolute values are < 0.1632993. Essentially I want a list of indices for each column. But my apply function doesn't seem to do what I wanted it to do.

Answer

We can use which with arr.ind=TRUE on the entire dataset to get the row/column indexes

which(abs(beta) < 0.1632993, arr.ind=TRUE)