【发布时间】:2020-10-31 22:10:21
【问题描述】:
我想根据comment_1和comment_2中的字符对我的df的数据进行过滤和分组,并给出指标0或1作为最终结果。但是,有一些规则与过滤器一起出现。规则是:
-
如果行的
comment_1由apple组成,而行的comment_2由apple组成,则为 好吧,然后使用price_1减去price_2。如果减法后的数大于 20,则result将是1,如果小于 20,则result将是0 -
如果行的
comment_1由orange组成并且comment_2由apple组成/comment_1由apple组成,comment_2由orange组成,然后也使用price_1减号price_2。如果 sbutraction 后的数字大于 10,则result将是1否则result将是0。
请注意,Apple 或 apple、Orange 或 Orange 无关紧要,因此代码也应考虑大写字母。
例如:
-
数据的第一行是
apple (comment_1)到Apple (comment_2),price_1减去price_2的结果是13小于20,所以result会显示为0。 -
第二行数据是橙色的(comment_1)
to Apple (comment_2),减去price_1和price_2后的结果是11,因为11大于10,所以最终的result会显示为1. -
由于第4行
price_1-price_2= 2小于10,所以结果为0。
我将我的 df 附在下面,最后的列结果是最终答案。
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)
【问题讨论】:
标签: r if-statement conditional-statements