mercredi 25 juillet 2018

Replace values in R dataframe based on conditions

In R, I'm using a for loop with an if statement to replace all values in a dataframe that fall outside of a certain range.

for (i in seq_along(df$Age)) {
  if (df$Age[[i]] > 90 || df$Age[[i]] < 16) {
    df$Age[[i]] <- NA
  }
}

This seems like clunky code. Is there a faster, easier way to do this?

Aucun commentaire:

Enregistrer un commentaire