Please note that soultions written above removes only space. If you want also to remove tab or new line use stri_replace_all_charclass
from stringi
package.
library(stringi)
stri_replace_all_charclass(" ala \t ma \n kota ", "\\p{WHITE_SPACE}", "")
## [1] "alamakota"