I have a data set that has muscle activity data from a group of athletes who have had an ACL reconstruction. I want to reassign the limb side to indicate the ACLR limb and uninjured limb. Looking at the dataset called EMG below, suppose John had a left ACLR and Bob a right ACLR.
athlete limb EMG_value
John left 0.8
John right 1.2
Bob left 0.5
Bob right 0.9
I would want the data set to look like this:
athlete limb EMG_value
John ACLR 0.8
John uninjured 1.2
Bob uninjured 0.5
Bob ACLR 0.9
My original plan was to subset the data by athlete, change the limb value, and then bind the data back into the original data set.
An excerpt from process this is as follows:
John = subset(EMG, athlete=="John")
John$side<- as.character(John$side)
John$side[John$side=="Left"]="ACLR"
John$side[John$side=="Right"]="Uninjured"
John$side = as.factor(John$side)
Bob = subset(EMG, athlete=="Bob")
Bob$side<- as.character(Bob$side)
Bob$side[Bob$side=="Left"]="Uninjured"
Bob$side[Bob$side=="Right"]="ACLR"
Bob$side = as.factor(Bob$side)
EMG2 = rbind(Bob, John)
I'm sure there is a way to do this more quickly using data piping in dplyr. I'm sure there is a way to replace the value of a variable based on a specified condition.
The logic would be: if athlete==Bob then replace Left with ACLR and replace Right with Uninjured.
Thanks for any help you can provide.
Matt
BTW: your logic and example contradict: you say "Bob" and "Left" means "ACLR" but your example data disagrees. Nonetheless:
library(dplyr)
## generalizable so you can easily add other patients, etc
leftAthletes <- c('Bob')
mutate(acl, limb=ifelse(xor(athlete %in% leftAthletes, limb == 'left'),
'uninjured', 'ACLR'))
## athlete limb EMG_value
## 1 John uninjured 0.8
## 2 John ACLR 1.2
## 3 Bob ACLR 0.5
## 4 Bob uninjured 0.9
(Note the use of xor
... the check within the ifelse
essentially says "if in leftAthletes and right limb, or not in leftAthletes and left limb, then uninjured else ACLR".)