0

In a dataframe like this:

data.frame(id = c("google", "Amazon"), text1 = c("Lee  erke rle  "," Elrl fe"), text2 = c(" Text  e2  ","text Els "))

Is there any command which can remove the spaces which are not useful/extra and convert to lower case all columns expect the first one?

Example output:

data.frame(id = c("google", "Amazon"), text1 = c("lee erke rle","elrl fe"), text2 = c("text e2 ","text els"))
Erik Brole
  • 315
  • 9
  • 1
    Try `df1[-1] <- lapply(df1[-1], \(x) trimws(tolower(x)))` – akrun Nov 22 '22 at 19:37
  • To also get rid of the inner multiple spaces try `lapply(df1[-1], \(x) trimws(tolower(gsub('\\s+', ' ', x))))` . – jay.sf Nov 22 '22 at 20:13
  • in order to change to lower (or upper) case, ```stringr``` is very useful, I'm sure you can also use it to remove the spaces, I just can't get the right function now, take a look at: https://evoldyn.gitlab.io/evomics-2018/ref-sheets/R_strings.pdf (ps: I'm wondering if you meant the rows and not the colunms since the colnames(data) are already in lower case with no spaces ?) take a look at the acepted answer here too: https://stackoverflow.com/questions/5992082/how-to-remove-all-whitespace-from-a-string – Larissa Cury Nov 22 '22 at 21:40
  • @LarissaCury Good when we look for duplicates; your suggestion, though, appears to remove all whitespace from the strings. – jay.sf Nov 22 '22 at 21:51

1 Answers1

1

Here a tidyverse approach:

df <-
  data.frame(
    id = c("google", "Amazon"),
    text1 = c("Lee  erke rle  "," Elrl fe"),
    text2 = c(" Text  e2  ","text Els ")
  )

library(dplyr)
library(stringr)


df %>% 
  mutate(across(
    .cols = starts_with("text"),
    .fns = ~str_to_lower(str_squish(.))
  ))

Output

      id        text1    text2
1 google lee erke rle  text e2
2 Amazon      elrl fe text els
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32