2017-02-27 130 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