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_1
of the row consists ofapple
and thecomment_2
of the row consists ofapple
as well, then useprice_1
minusprice_2
. If the number after subtraction is greater than 20, then theresult
will be1
, if less than 20, then theresult
will be0
If the
comment_1
of the row consists oforange
andcomment_2
consists ofapple
/ thecomment_1
consist ofapple
andcomment_2
consist oforange
, then also useprice_1
minusprice_2
. If the number after sbutraction is greater than 10, then theresult
wil be1
otherwise theresult
will 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_1
minusprice_2
is 13 which is smaller than 20, hence theresult
will be shown as0
.2nd row of the data is orange (comment_1)
to Apple (comment_2)
and the result is11
after minusprice_1
withprice_2
, since 11 is greater than 10, so the finalresult
will be shown as1
.Since the 4th row
price_1
-
price_2
= 2
which 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)