2017-02-27 216 views
0

與價值觀創建數據幀新的專欄中,我在爲r的數據幀:基於其他列

  word  positive.polarity negative.polarity 
1 interesting     1     0       
2  boring     0     1 

我嘗試添加一個名爲positive.ponderate.polarity新列包含positive.polarity的價值* 3如果上下文od字包含特殊字符,並且positive.polarity/3如果不是。

任何想法請做到這一點?

謝謝

+0

劃分可以使用'ifelse'。順便說一下,特殊字符是什麼? – akrun

回答

2

不知道你的「特殊字符」 ......我要使用的條件是什麼:"[o]{2}|[y]$"或基本術語

如果單詞包含兩個「O公司「或以'y'結尾:乘以3;如果不是3

使用tm包爲stopwordspackage::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