dimanche 1 mars 2020

Invalid factor level, NA generated (if-else statement won't work) in R

I am currently trying to solve an if-else problem. I have one column in my data frame which contains numbers: the column is called "sentiment". I want to add a second column for which every row gets assigned a word: "positive", "negative", or "neutral". This column I call "evaluation". The criteria is that the column evaluation must hold the word "positive" if the sentiment column of that row contains a number above 0.25, "negative" if it holds a number below -0.25, and "neutral" otherwise. I tried running the following if-else construction:

here's the code Subsequently, I would bind the evaluation vector with my existing data.frame, but that is not the issue, I know how to do that. But this statement generates "neutral" fine, but for every row that should be "positive" or "negative", an NA appears. I have no idea how to solve it. I am quite new to R, but am desperate for your help. "Invalid factor level, NA generated" is a warning I get. The problem does not seem to be in the fact that the column is not of a numeric type, because it is.

Aucun commentaire:

Enregistrer un commentaire