1

I have text data as given below:

\r\n    \r\n        How to get a confirm ticket?\r\n        \r\n            I want to get a tatkal ticket confirm ...

How to extract two columns from this data?

I have tried str_split_fixed() which divide into four columns and after those four column, two column can be retrieved ... But I want that straight forward it gives only two columns.

x <- "\r\n    \r\n        How to get a confirm ticket?\r\n        \r\n            I want to get a tatkal ticket confirm ..."
str_split_fixed(x, "\r\n", 4)
#>      [,1] [,2]   [,3]                                   [,4]                                                               
#> [1,] ""   "    " "        How to get a confirm ticket?" "        \r\n            I want to get a tatkal ticket confirm ..."
str_split_fixed(x, "\r\n", 4)[1, 3]
#> [1] "        How to get a confirm ticket?"

1 Answer 1

1

If the strings are always in that same format, the following regular expression should work well:

library(stringr)
x <- "\r\n    \r\n        How to get a confirm ticket?\r\n        \r\n            I want to get a tatkal ticket confirm ..."
str_split(x, "(\r\n\\s*)+", simplify = TRUE)[, -1, drop = FALSE]
     [,1]                           [,2]                                       
[1,] "How to get a confirm ticket?" "I want to get a tatkal ticket confirm ..."

If your data actually comes from a table in a text file or from a web page, there are probably more convenient options.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I have another string : 500words\n \n \n \n \n \n Author\n \n
how to do for this
I would recommend learning regular expressions (e.g., link). Here, the pattern separating the strings seems to be something like "(\n\\s*)+"?!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.