0

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.

bea
  • 123
  • 9
  • Please make a reproducible example. Read this: [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/10068985) – Darren Tsai Feb 25 '19 at 16:42
  • Try `library(tidyverse); gather(df2) %>% filter(value != "") %>% separate_rows(value)` – akrun Feb 25 '19 at 16:46

2 Answers2

1

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.

Vandenman
  • 3,046
  • 20
  • 33
1

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))))
TinglTanglBob
  • 627
  • 1
  • 4
  • 14