I want to extract the positions which are higher than 10 from row 3,6,9 and column 2,3. Any suggestions of how to do this? Many thanks!
df <- data.frame(a=c(1,3,99,45,67,5,34,1,6), b=c(45,8,79,3,12,76,9,10,87),c=c(46,7,21,9,89,34,1,67,56),d=c(56,1,1,76,42,78,98,4,8))
> df
a b c d
1 1 45 46 56
2 3 8 7 1
3 99 79 21 1
4 45 3 9 76
5 67 12 89 42
6 5 76 34 78
7 34 9 1 98
8 1 10 67 4
9 6 87 56 8
Wanted output:
row column
[1,] 3 2
[2,] 3 3
[3,] 6 2
[4,] 6 3
[5,] 9 2
[6,] 9 3
CodePudding user response:
We may use which
with arr.ind
on a logical matrix
to get the row/column
indexes. Then subset the rows of the index matrix based on the values in the row/col
values
ind <- which(df > 10, arr.ind = TRUE)
ind[ind[,1] %in% c(3, 6, 9) & ind[,2] %in% 2:3,]
-output
row col
[1,] 3 2
[2,] 6 2
[3,] 9 2
[4,] 3 3
[5,] 6 3
[6,] 9 3