Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing the last paragraph of a text in R

My Dataframe with around 200 entries is build as follows:

text <- as.data.frame("Lorem ipsum dolor sit amet, consetetur \n sadipscing elitr, sed diam nonumy eirmod tempor invidunt \n ut labore et dolore magna aliquyam erat, sed diam voluptua.\nAt vero eos et accusam et justo duo dolores et ea rebum.")
colnames(text) = "Lorem"

I am trying to delete the last paragraph of every entry.
All of which are diffrent in text and length.
My latest way of trying was to find a way to sub everything after the last linebreak.

text %>% mutate(Lorem = sub("\n{.,}$","", Lorem))

I have tried to find the right kind of RegEx but seem to be unsuccseful in doing so.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have been able to create the opposite of what i need.

text %>% mutate(Lorem = sub(".*\n","", Lorem))

the result being: "At vero eos et accusam et justo duo dolores et ea rebum."

But also cant seem to find my way to the right negation for this term.

For this example the result would be: "Lorem ipsum dolor sit amet, consetetur \n sadipscing elitr, sed diam nonumy eirmod tempor invidunt \n ut labore et dolore magna aliquyam erat, sed diam voluptua."

>Solution :

You could do:

text %>% mutate(Lorem = sub("\n[^\n]+$", "", Lorem))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading