I have a matrix:
1 3 NA
1 2 0
1 7 2
1 5 NA
1 9 5
1 6 3
2 5 2
2 6 1
3 NA 4
4 2 9
...
I would like to select those elements for each number in the first column to which the corresponding value in the second column has an NA in its own second column.
So the search would go the following way:
- look up number in the first column: 1.
- check corresponding values in second column: 3,2,7,5,9,6…
- look up 3,2,7,5,9,6 in first column and see if they have NA in their
second column
The result in the above case would be:
>3 NA 4<
Since this is the only value which has NA in its own second row.
Here’s what I want to do in words:
-
Look at the number in column one, I find ‘1’.
-
What numbers does 1 have in its second column: 3,2,7,5,9,6
-
Do these numbers have NA in their own second column? yes, 3 has an NA
-
I would like it to return those numbers not row numbers.
-
the result would be the subset of the original matrix with those rows which satisfy the condition.
This would be the matlab equivalent, where i is the number in column 1:
isnan(matrix(matrix(:,1)==i,2))==1)
Using
by, to get the result by group of column 1, assumingdatis your data frameEDIT
Here I trie to do the same function as matlab command:
here the R equivalent of matlab