This should do the trick for now:
items <- c("coke", "tea", "shampoo","aspirin")
# scores for each item
score <- as.numeric(c(65,30,45,20))
Try using data.frame
instead of as.data.frame
. Using the latter causes the values to be converted to factors
# making a data frame of the two vectors created
df <- data.frame(items, score)
df
items score
1 coke 65
2 tea 30
3 shampoo 45
4 aspirin 20
# score for coke is 65 and for tea it is 30. I want to
# double score for tea OR coke if the score is below 50
df$score[df$items %in% c("coke", "tea")] = ifelse(df$score[df$items %in% c("coke", "tea")] < 50, df$score*2, df$score)
df
items score
1 coke 65
2 tea 60
3 shampoo 45
4 aspirin 20
This method doesn't work if you end up having duplicate entries for for items though.
# New data with an added entry for item = coke and score = 15:
items <- c("coke", "tea", "shampoo","aspirin","coke")
# scores for each item
score <- c(65,30,45,20,15)
# making a data frame of the two vectors created
df <- data.frame(items, score)
# using the method from above the last entry get converted to a value of 90
# instead of 30
df$score[df$items %in% c("coke", "tea")] = ifelse(df$score[df$items %in% c("coke", "tea")] < 50, df$score*2, df$score)
df
items score
1 coke 65
2 tea 60
3 shampoo 45
4 aspirin 20
5 coke 90
So if you have any cases where you may have duplicate entries you will have to use this method
df <- data.frame(items, score)
df$score[df$items %in% c("coke", "tea") & df$score < 50] <- 2* df$score[df$items %in% c("coke", "tea") & df$score < 50]
df
items score
1 coke 65
2 tea 60
3 shampoo 45
4 aspirin 20
5 coke 30