I have dataframe that looks like this:
negative positive
1: enjoyed
2: hate famous,helpful
3: forget,poor
4: hate enjoyed, kind
I want to convert it into something like this:
text sentiment
1 hate negative
2 forget negative
3 poor negative
4 enjoyed positive
5 famous positive
6 helpful positive
7 kind positive
Appreciate your kind help.
How about something like:
df0 <- data.frame(
negative = c("", "hate", "forget,poor", "hate"),
positive = c("enjoyed", "famous,helpful", "", "enjoyed, kind"),
stringsAsFactors = FALSE
)
values <- sapply(df0, function(x) unique(trimws(unlist(strsplit(x, ",")))))
df1 <- data.frame(
text = unlist(values),
sentiment = rep(c("negative", "positive"), lengths(values)),
row.names = NULL
)
df1
note that I used stringsAsFactors = FALSE
, if your variables are factors you'll have to convert them to strings first.
You could try something like this:
# create testdat
test_data <- data.frame(negative = c("", "hate", "forget, poor", "hate"), positive = c("enjoyed", "famous, helpful", "", "enjoyed, kind"), stringsAsFactors = F)
#extract positive and negative colum and split along ", "
neg <- unique(unlist(strsplit(test_data$negative, ", ")))
pos <- unique(unlist(strsplit(test_data$positive, ", ")))
# combine neg and positive into a dataframe and add the sentiment column
combined <- data.frame(text = c(pos, neg), sentiment = c(rep("positive", length(pos)), rep("negative", length(neg))))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.