samedi 23 septembre 2017

error in separating text from a string using regex and ifelse condition

What I want to do is to strip text from a string where ever there is an ":".

Suppose my text contains:

 text$Text[[3]] = "There is a horror movie running in the iNox theater. : Can we go?"

And what I want to create a dataframe such that:

  Col1                                                    Col2
  There is a horror movie running in the iNox theater.    Can we go?

I am trying to use the following :

 df = data.frame(Text = strsplit(text$Text[[3]], 
                 ifelse(":", ":", text$Text[[3]]))[[1]], stringsAsFactors = F)

dat3$Text[[3]] because the text is in row no. 3 of the text$Text.

But the above ifelse() logic did not work. Here I was trying to use ifelse condition such that if there is a ":" in the text, use ":" otherwise use the complete text as it is. So it means in case if there is no ":" then the result would look like something:

 text$Text[[3]] = "Hi Mom, You there. Can I go to Jimmy's house?"

 Col1                                                 Col2
 Hi Mom, You there. Can I go to Jimmy's house?         NA

How to do it correctly?

Aucun commentaire:

Enregistrer un commentaire