Remove spaces for values in a column

So I have a some data coming in where the spacing is not consistent for one particular column as a result I am not able to group them by this particular column.

testdata <- tibble::tribble(
  ~config,       ~construct, ~var,
  1, "This is line 1",   12,
  2, " This is line 2",   15,
  3, "This is line   1 ",   21,
  4, "This  is line 2",   12,
  5, "This is line 3",   12,
  6, "This  is line 4",   11,
  7, " This   is line 3 ",   21,
  8, "This is   line 4",   12
)

As you can see df above I am trying to group them by construct but then since the spacing is not consistent I am not sure how do I trim this in order to group them properly.

I have looked into trim but it appears to remove only head and trail space and not take care of the additional spaces in between. How can I remove such spaces in this case.

The stringr package has a function called str_squish() that will both trim the leading spaces and remove duplicate spaces within the string.

library(stringr)
library(dplyr)
testdata <- testdata %>% mutate(construct = str_squish(construct))
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.