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.
  • 16,858
  • 11
  • 64
  • 99
Marcin Kosiński
  • 7,578
  • 5
  • 49
  • 95
  • 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,575
  • 4
  • 51
  • 82
1
^\s*(?=\S)

This should do it.Replace by empty string.

or

^\\s*(?=\\S) for r
vks
  • 65,133
  • 10
  • 87
  • 119