Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove line breaks from a string?

I've am extracting tweets using the Twitter API in R. I have been saving my results to csv in r using a write.csv2 command which is fine but there is an issue where character returns in the tweet text are causing the multiple rows in the spreadsheet for the one tweet.

I've tried using a str_replace_all but it doesn't seem to work for me and i can't find anything as to why.

Here is my code

searchTags = c("Galwaybikeshare", "Corkbikeshare", "dublinbikes", "BelfastBikes", "SantanderCycles", "CitiBikeNYC", "obike", "Hubway", "bicing")

additionalParams = c("-rt -http")

searchString <- paste((paste(searchTags[1:9], collapse = " OR ")), additionalParams, collapse = "")

tweets_list <- searchTwitter(searchString, n=20, lang = "en", resultType = 'recent')

str_replace_all(tweets_list, "[\r\n]" , "")

tweets.df <- twListToDF(tweets_list)

todayDate <- Sys.Date()

tweetArchive <- paste("BikeShareTweets ", todayDate, ".csv", sep ="")

write.csv2(tweets.df, file = tweetArchive)

The text below is an example of a tweet which is causing the issue.

"TransitNinja205: 0.01% of the budget for 5-borough @CitiBikeNYC,\nand 0.2% for #FairFares. @NYCmayor @NYCmayorsOffice #progressive"

Why isn't my str_replace_all removing the \n from the text?

like image 969
Brian Lee Avatar asked Oct 31 '25 12:10

Brian Lee


1 Answers

stringr::str_replace_all works, you’re just ignoring the result. To fix it:

tweets_list = str_replace_all(tweets_list, "[\r\n]" , "")
like image 126
Konrad Rudolph Avatar answered Nov 03 '25 04:11

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!