Let's say I have a dataframe:
word <- c("good", "great", "bad", "poor", "eh")
userid <- c(1, 2, 3, 4, 5)
d <- data.frame(userid, word)
I want to add a dataframe column, sentiment, that is a factor and depends on what word is:
words_pos <- c("good", "great")
words_neg <- c("bad", "poor")
calculate_sentiment <- function(x) {
if (x %in% words_pos) {
return("pos")
} else if (x %in% words_neg) {
return("neg")
}
return(NA)
}
d$sentiment <- apply(d, 1, function(x) calculate_sentiment(x['word'])
However, now d$sentiment is of type "character". How do I make it a factor with the right levels? pos, neg, NA -- I'm not even sure if NA should be a factor level, as I'm just learning R.
Thanks!
addNAinstead offactor. Something likeaddNA(sapply(word, calculate_sentiment)). Not to mention that you probably could easily vectorize this too.