mardi 26 février 2019

R: using FOR-loop and IF-ELSE constructs to omit odd numbers in column

For a project, I was asked to create a FOR-loop with IF-ELSE construct where I omit the odd numbers in a column of the dataframe. However, when I was trying to construct it I couldn't get the right answer. This is how my code looks like:

for(i in 1:nrow(my_columns)) {
if (my_columns$id[[i]] %% 2 != 0) {
my_columns$id <- my_columns$id[[-i]]
  }
}

Then R returns this error message:

 Error in my_columns$id[[-i]] : 
 attempt to select more than one element in integerOneIndex

I think I am making a mistake in the part where the if statement has to return a value. Could you please tell me what my mistake is? Moreover, can you help me create the for-loop?

Thank you!

Aucun commentaire:

Enregistrer un commentaire