Search code examples
rgsubline-breaks

What is wrong with my R codes for removing line breaks in a column?


I have a dataframe called "df00" and it has the first column named as "Room type". This column has a lot of line breaks in it.

I need to make a copy of this dataframe (let's call it df00_dup) and add a duplicate of the first column in this new dataframe. The duplicated column will be named "RoomInfo". I need to remove the line breaks found in the column "RoomInfo".

Here is an extract of the first column of df00:

Room type
King Room with Balcony...
King Room with Balcony...
Deluxe Room...
Deluxe Room...

Column RoomInfo in df00_dup is, of course, the same as above.

My R codes to remove those line breaks currently stand as follows:

df00_dup <- df00
df00_dup$RoomInfo <- gsub("[\r\n]", "", df00[1])

When I run the second line from the above codes, the column RoomInfo now displays the following:

RoomInfo
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...

All rows of the RoomInfo column is now filled with the above.

Expected output:

RoomInfo
King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
Deluxe Room\n\n\n\n\n\n\n\n\n\n\n...
Deluxe Room\n\n\n\n\n\n\n\n\n\n\n...

What is wrong with my codes? How can I remove those line breaks by maintaining the correct mapping?

Added (dput):

structure(list(`Room type` = c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n2 large double beds\n\n\n\n\n\n\n\n\n50 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser", 
"King Room with Balcony\n\n\n\n\n\n\n\n\n\n2 large double beds\n\n\n\n\n\n\n\n\n50 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser", 
"Deluxe Family Room\n\n\n\n\n\n\n\n\n\n2 extra-large double beds\n\n\n\n\n\n\n\n\n70 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nSofa\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser\n\n\n\nMore", 
"Deluxe Family Room\n\n\n\n\n\n\n\n\n\n2 extra-large double beds\n\n\n\n\n\n\n\n\n70 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nSofa\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser\n\n\n\nMore", 
"Deluxe Family Room\n\n\n\n\n\n\n\n\n\n2 extra-large double beds\n\n\n\n\n\n\n\n\n70 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nSofa\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser\n\n\n\nMore"
), `Price for 2 nights` = c("MUR 46,748\n\n\n\nPrice\nMUR 46,748\n\n\n\n\n+MUR 4,030 taxes and charges", 
"MUR 23,374\n\n\n\nPrice\nMUR 23,374\n\n\n\n\n+MUR 2,015 taxes and charges", 
"MUR 59,334\n\n\n\nPrice\nMUR 59,334\n\n\n\n\n+MUR 5,115 taxes and charges", 
"MUR 89,001\n\n\n\nPrice\nMUR 89,001\n\n\n\n\n+MUR 7,672 taxes and charges", 
"MUR 29,667\n\n\n\nPrice\nMUR 29,667\n\n\n\n\n+MUR 2,557 taxes and charges"
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))

Solution

  • We would need to extract the column as a vector as df00[1] would still be a data.frame with a single column and gsub/sub expects a vector as input

    df00_dup$RoomInfo <- gsub("[\r\n]", "", df00[[1]])