lundi 30 août 2021

How to produce an if statement to output the respective row [duplicate]

I have been working on some r code for a while, in which I am printing out the rows that have repeated cells, for example:

dataframe
Peptide | Domain | Count | Terminal 
AT1G4561  TH541     5        N
AT1G4561  RE123     6        N
AT4G6789  RT990     2        C
ATM43312  TH324     1        N

I have produced for loops with nested if statements to run through this dataframe (about 60,000 rows long) to keep only those with repeated 'Peptide' names. For example the data kept and printed here would be:

Peptide |
AT1G4561
AT1G4561

I have used this code so far:

new_res <- list()
for (i in 1:nrow(dataframe)) {
  if(isTRUE(dataframe$Peptide[i+1] == dataframe$Peptide[i])) {
    output <- print(dataframe$Peptide[i+1])
    new_res[[i+1]] <- output
    output <- print(dataframe$Peptide[i])
    new_res[[i]] <- output
  }
}

This outputs all of the peptide names that appear more than once into the list new_res. However I want to output the entire row containing the respective peptides. So I would have this as an output instead of just the peptide names:

new_res
Peptide | Domain | Count | Terminal 
AT1G4561  TH541     5        N
AT1G4561  RE123     6        N

I've tried to get this to work but I haven't had any success so far. Please could someone help me to figure out how I could go about this? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire