不知道你的「特殊字符」 ......我要使用的條件是什麼:"[o]{2}|[y]$"
或基本術語
如果單詞包含兩個「O公司「或以'y'結尾:乘以3;如果不是3
使用tm
包爲stopwords
和package::dplyr
# Created some data to mimic yours
var_df <- data.frame(word = tm::stopwords(),
stringsAsFactors = FALSE) %>% mutate(
positive.polarity = sample(0:1, nrow(.), TRUE)) %>% mutate(
negative.polarity = ifelse(positive.polarity == 1, 0, 1)
) %>%
# Applying the condition and evaluating the variable formula if met
mutate(
positive.ponderate.polarity = ifelse(
grepl("[o]{2}|[y]$", word),
positive.polarity * 3,
positive.polarity/3)
)
tail(var_df, 10)
word positive.polarity negative.polarity positive.ponderate.polarity
165 no 0 1 0.0000000
166 nor 0 1 0.0000000
167 not 1 0 0.3333333
168 only 1 0 3.0000000
169 own 1 0 0.3333333
170 same 1 0 0.3333333
171 so 0 1 0.0000000
172 than 1 0 0.3333333
173 too 1 0 3.0000000
174 very 1 0 3.0000000
劃分可以使用'ifelse'。順便說一下,特殊字符是什麼? – akrun