mardi 28 avril 2020

Populate a column in R [using if statements?] based on variable in another column

A seemingly simple task that I am struggling with:

I collected behavior data and have a column ("Behavior") in which I recorded what behavior is taking place. I would like to group these behaviors into categories and fill in a column ("BehaviorGroup") with the category name.

An example of how this might look:

Behavior      BehaviorGroup
bite          aggressive contact
ram           aggressive contact
avoid         avoid
fast-approach aggressive approach
flee          avoid
fast-approach aggressive approach

etc. 

So I would like to use the behaviors listed under the Behavior column (which I already have) to fill in the BehaviorGroup column (which is currently empty) based on the category.

For the example above: "bite" and "ram" are "aggressive contact" "avoid" and "flee" are "avoid" "fast-approach" is "aggressive approach"

I have MANY more but I'm hoping this is enough to get started on how to tackle the code to do this! I would greatly appreciate any and all help with this!

Here's code for what I would be working with, based on the example given:

Behavior <- c("bite", "ram", "avoid", "fast-approach", "flee", "fast-approach")
BehaviorGroup <- c("NA", "NA", "NA", "NA", "NA", "NA")
data.frame(Behavior,BehaviorGroup)

I'm just trying to fill in the BehaviorGroup column Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire