I was wondering if anyone has a good way to achieve this. I have a data frame where each observation (= element) belonging to a specific group (= condition) has a given value:
# Create sample data. item = rep(1:3,2) #6 items condition = c(rep("control",3), rep("related",3)) #2 conditions value = c(10,11,12,20,21,22) #6 values df = data.frame(item, condition, value) item condition value 1 1 control 10 2 2 control 11 3 3 control 12 4 1 related 20 5 2 related 21 6 3 related 22
I also have a lookup table containing the average for each group:
# Create lookup table. condition = c("control", "related") mean = c(11,21) table = data.frame(condition, mean) condition mean 1 control 11 2 related 21
I want to change the original data frame so that it contains a new label column , which says βlowβ if the element value is less than the group otherwise βhighβ. It should look like this:
# How the output should look like. # If the item value is less than the group mean, write "low". Write "high" otherwise. item = rep(1:3,2) condition = c(rep("control",3), rep("related",3)) value = c(10,11,12,20,21,22) label = c(rep(c("low", "high", "high"),2)) output = data.frame(item, condition, value, label) item condition value label 1 1 control 10 low 2 2 control 11 high 3 3 control 12 high 4 1 related 20 low 5 2 related 21 high 6 3 related 22 high
If it were just copying the group value into the original data frame, I would use merge . But I need to consider the value of the group in order to write a new label for each element that says βlowβ or βhighβ depending on the average value of the group.
One thing I tried was to first merge my data frame with a table, and then use ifelse to compare the column value with the average column. This works, but I also get the middle column in my data frame, which I don't need (I only need the label column). Of course, I can delete the middle column manually, but it seems awkward. So I was wondering: does anyone know a better / more elegant solution?
Thanks a bunch!