mardi 18 mai 2021

Is there a way to use if-else statement for finding columns with the smallest value in df?

Could you please help me with the following question?

This is a reproducible example of df in R that I have:

name <- c("cat", "dog", "fish", "chicken", "dino")
age <- c(29, 30, 100, 12, 1000)
start_pos <- c(1, 2, 3, 4, 5)
end_pos <- c(1, 2, 5, 7, 5)


df <- data.frame(name, age, start_pos, end_pos,
                                      stringsAsFactors = F)

df

I am trying to sort out this data so that I have in my df the columns only if their end_pos-start_pos equals 0. So, only a cat, dog and dino are supposed to stay in df.

I was thinking of doing it using if else statement, like

if(end_pos-start_pos=0){
  print("yes")
}

However, the errors started to appear. Is it a good way to use if else statement for solving this task? Could you please recommend me another way of thinking about this task?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire