-3

I am working on a project in R (on TED_Talks data set). I have a data frame with one column called "tags" which contains a character like

"gaming,gender,sex,feminism,education,culture".

The problem is, the whole row is being read as a single character.

I want the output to be a vector containing separate words. eg:

"gaming","gender","sex","feminism","education","culture"

so I can do further analysis on tags.

  • 2
    If your project is in R, why do you tag the question with "python"? And what exactly is your question? – DYZ Feb 21 '18 at 04:42
  • My requirement is entirely different one. I am not trying to split the column into separate columns. I am trying to split the words."gaming,gender,sex,feminism,education,culture" to separate words:"gaming","gender","sex","feminism","education","culture" and store them in the same column. I want to do this operation to all columns . – Julapalli Harish Feb 21 '18 at 04:49
  • @JulapalliHarish I have reopened the question. So do you want to keep the words in the same column but as character vector? Try with `df$new_col <- strsplit(df$old_col, ",")[[1]]` – Ronak Shah Feb 21 '18 at 04:51
  • Basically you need to do this https://stackoverflow.com/questions/43877172/split-comma-delimited-string-in-r but for multiple columns. – Ronak Shah Feb 21 '18 at 05:03
  • Since it is one column, it will be read as such. Only if you want to separate it into different columns then. Or nest the words within the column – Onyambu Feb 21 '18 at 05:05
  • @Ronak When I run the above line of code on the entire table, I am getting a row like this: c("cars", "alternative energy", "culture", "politics"). Is it possible to retain the column with just words in it? Like:"cars", "alternative energy", "culture", "politics". – Julapalli Harish Feb 21 '18 at 05:25

1 Answers1

0

You can simply do the following: say your entry is in object a, and you want to allocate the final result to object b:

a <- "gaming,gender,sex,feminism,education,culture"
b <- unlist(strsplit(a, "[,]"))
Mahdi Baghbanzadeh
  • 540
  • 1
  • 4
  • 10