-2

Suppose I have a matrix:

    Pears Blueberries Oranges Apricot Apple
Pears   1       0          0       1      0

Blueberries 1 1 1 1 0

I want to list out the elements that have "1" in row "Pears" and "Blueberries". I want to obtain some outcome like: Set for pears = Pears, Apricot Set for blueberries = Pears, Blueberries, Oranges, Apricot

Is there any code that can help me to achieve that?

Thank you.

Ben
  • 1
  • 1
  • 2
    I think the solution for your problem is [here](https://stackoverflow.com/questions/5391124/select-rows-of-a-matrix-that-meet-a-condition/5391697) – Adamm Oct 11 '19 at 07:07
  • 2
    Your example very simple and I'm not sure if I understand it fully. Can you clarify by giving perhaps a few more lines of data and explaining what the desired output is? If you feel solutions proposed in answers linked by Adamm do not address your problem, ping me and we'll reconsider opening the question. – Roman Luštrik Oct 11 '19 at 07:36

1 Answers1

0

Your question is not very clear but if you want to list the row with Pears==1, you can use data.table

library(data.table)
DATA=data.table(DATA)
DATA[Pears==1,]
Félix Cuneo
  • 159
  • 6
  • 3
    Not sure of why someone down-voted, but I have to ask. Why a data.table solution for the most basic R functionality? – Roman Luštrik Oct 11 '19 at 07:27
  • Because readability is much superior. And anyway the matrix needed to be converted to a data.frame, so, it's one more line but a DATA$ less ! – Félix Cuneo Oct 11 '19 at 07:30