如何将我的数据框数值分类为正面和负面字符

时间:2017-05-17 08:13:21

标签: r

我有一个名为sentiments的数据框:

sentiments <- data.frame(vals = c(0.2425356,0.4472136,0.4472136,0.0000000,0.0000000,0.4472136,0.4472136,
                              0.4472136,0.4472136,0.0000000,0.0000000,0.0000000,0.8956686,0.0000000, 
                              0.5692100,0.0000000,0.0000000,0.5692100,0.0000000,0.0000000,0.3535534,0.0000000,0.5000000,0.0000000,0.9333333,
                              0.3015113,0.3015113,0.4472136,0.0000000,0.7071068,0.4472136,0.3015113,0.0000000,0.4472136,0.4472136,0.9333333,
                              -0.4365641,0.2500000,0.0000000,0.0000000,0.0000000,0.0000000,0.2672612,-0.5773503,0.3015113,0.2672612,0.0000000,0.0000000,0.3333333))

当我发出命令时:

sentiments[["polarity"]] <- cut(sentiments[49,], 
                                c(-0.64,0.0,1.2), 
                                labels = c("negative","positive"))

table(sentiments$polarity)
# negative positive 
# 0        49

我看到我的条件不适用于削减数据帧,如 所有值都变为正值。我想将所有负值分类为“负”,将所有高于零的值分类为正值。 感谢您的回复。

1 个答案:

答案 0 :(得分:0)

您仅在第49行情绪中使用cut()sentiments[49,]

改为使用:

sentiments[["polarity"]] <- cut(sentiments[,1], 
                            c(-0.64,0.0,1.2), 
                            labels = c("negative","positive"))
table(sentiments$polarity)
# negative positive 
# 22       27