0

Does anyone know how to remove whitespaces at the beggining of those characters using regexes in R?

 c( "bundesliga" ,                                                                                                                                                                                                              
" liga niemiecka"  ,                                                                                                                                                                                                                          
"   wyniki na żywo" )

This doesn't seem to work:

lowered <- c( "bundesliga" , " liga niemiecka", "   wyniki na żywo" )
grep( x = lowered, pattern = "^\\s*(?=\\S)", value = TRUE, perl = TRUE )

   [1] "bundesliga"        " liga niemiecka"   "   wyniki na żywo"
Karsten W.
  • 17,826
  • 11
  • 69
  • 103
Marcin
  • 7,834
  • 8
  • 52
  • 99
  • 1
    `str_trim` from `library(stringr)` is a convenient function to use here. `str_trim(v1)` – akrun Mar 11 '15 at 10:30
  • 1
    If you are using `read.table` (or any `read.xxx`), you may set `strip.white = TRUE` - "allows the stripping of leading and trailing white space from unquoted character fields" – Henrik Mar 11 '15 at 12:06

2 Answers2

3

You can use gsub:

gsub("^\\s*", "", c( "bundesliga", " liga niemiecka", "   wyniki na żywo"))
#[1] "bundesliga"     "liga niemiecka" "wyniki na zywo"
Cath
  • 23,906
  • 5
  • 52
  • 86
1
^\s*(?=\S)

This should do it.Replace by empty string.

or

^\\s*(?=\\S) for r
vks
  • 67,027
  • 10
  • 91
  • 124