Suppose I have a data frame similar to this, only with 1000's of observations:
df <- data.frame(Group = c('A', 'A', 'A', 'B', 'B',
'B','B','C','C','C','D','D','D','D','D'),
Values=c('5','7','9','0','8','4','5','2','1','3','6','3','1','3','5'))
What I want to do is add a new calculated group to the data frame based on values in a group that already exists in the data frame without replacing the original group's values. For example, lets say I want to retain group D, but create a new group with all of group D's values +2.
An example of the resulting dataframe I would like is the following:
df <- data.frame(Group = c('A', 'A', 'A', 'B', 'B',
'B','B','C','C','C','D','D','D','D','D'
,'Dadjusted','Dadjusted','Dadjusted','Dadjusted','Dadjusted'),
Values=c('5','7','9','0','8','4','5','2','1','3','6','3','1','3','5',
'8','5','3','5','7'))
I have tried using ifelse statements like the following:
df$adjustedvalues<-ifelse(Group=='D', df$Values+2, df$Values)
but this approach results in data frames that look like the following:
df <- data.frame(Group = c('A', 'A', 'A', 'B', 'B',
'B','B','C','C','C','D','D','D','D','D'),
Values=c('5','7','9','0','8','4','5','2','1','3','6','3','1','3','5')
adjustedvalues=c('5','7','9','0','8','4','5','2','1','3','8','5','3','5','7')
Which is less than ideal for my purposes.