I want to filter and group the data of my df according to the character in the comment_1 and comment_2 and give the indicator 0 or 1 as a final result. However, there are some rules that come together with the filter. The rules are:
If the
comment_1of the row consists ofappleand thecomment_2of the row consists ofappleas well, then useprice_1minusprice_2. If the number after subtraction is greater than 20, then theresultwill be1, if less than 20, then theresultwill be0If the
comment_1of the row consists oforangeandcomment_2consists ofapple/ thecomment_1consist ofappleandcomment_2consist oforange, then also useprice_1minusprice_2. If the number after sbutraction is greater than 10, then theresultwil be1otherwise theresultwill be0.
Take note that the it doesn't matter is Apple or apple, Orange or orange, so the code should take capital letter into consideration as well.
For example:
1st row of the data is
apple (comment_1)toApple (comment_2), and the result ofprice_1minusprice_2is 13 which is smaller than 20, hence theresultwill be shown as0.2nd row of the data is orange (comment_1)
to Apple (comment_2)and the result is11after minusprice_1withprice_2, since 11 is greater than 10, so the finalresultwill be shown as1.Since the 4th row
price_1-price_2= 2which is smaller than 10, so the result is0.
I attached my df as below and the final column result is the final answer.
price_1 <- c(25, 33, 54, 24)
price_2 <- c(12, 22, 11, 22)
itemid <- c(22203, 44412,55364, 552115)
itembds <- as.integer(c("", 21344, "", ""))
comment_1 <- c("The apple is expensive", "The orange is sweet", "The Apple is nice", "the apple is not nice")
comment_2 <- c("23 The Apple was beside me", "The Apple was left behind", "The apple had rotten", "the Orange should be fine" )
result <- c(0, 1, 1, 0)
df <- data.frame(price_1, price_2, itemid, itembds, comment_1, comment_2, result)
