mercredi 28 juillet 2021

Matching and returning values based on condition or ID

This seems like it should be fairly easy, but i'm having trouble with it.

Example: I have a dataframe with two columns IDs and perc_change. I want to know which unique IDs have had more than 30% change.

IDs <- c(1,1,2,1,1,2,2,2,3,2,3,4,5,6,3)
perc_change <- c(50,40,60,70,80,30,20,40,23,25,10,30,12,7,70)
df <- data.frame(IDs, perc_change)

So far:

if (df$perc_change > 30) {
unique(df$IDs)
} else {
}

This obviously doesn't work because it returns all unique IDs. Should I be be finding the index and then matching it or soemthing?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire