# [R] sub function problem

Massimiliano Tripoli mtripoli at istat.it
Fri Feb 28 17:44:16 CET 2014

```Dear all,

I write a R function that it should handles strings like removing spaces at the beginning or
returning NA according an IF condition. Here you are the code:

corregge.CF <- function(stringa){
nc <- nchar(stringa)
stringa <- sub("^ ", "", stringa)
stringa <- sub(" +\$", "", stringa)
if (nc == 1) stringa <- NA
if (nc == 2) stringa <- NA
if (nc == 3) stringa <- NA
if (nc == 4) stringa <- NA
if (nc == 5) stringa <- NA
if (nc == 6) stringa <- NA
if (nc == 7) stringa <- NA
if (nc == 8) stringa <- NA
if (nc == 9) stringa <- paste("0",stringa,sep="")
if (nc == 10) stringa <- paste("00",stringa,sep="")
stringa
}

> corregge.CF <- function(stringa){
+ nc <- nchar(stringa)
+      stringa <- sub("^ ", "", stringa)
+      stringa <- sub(" +\$", "", stringa)
+ if (nc == 1) stringa <- NA
+ if (nc == 2) stringa <- NA
+ if (nc == 3) stringa <- NA
+ if (nc == 4) stringa <- NA
+ if (nc == 5) stringa <- NA
+ if (nc == 6) stringa <- NA
+ if (nc == 7) stringa <- NA
+ if (nc == 8) stringa <- NA
+ if (nc == 9) stringa <- paste("0",stringa,sep="")
+ if (nc == 10) stringa <- paste("00",stringa,sep="")
+                 stringa
+ }
> corregge.CF (" fsfhsfh")
[1] NA
> nchar(" fsfhsfh")
[1] 8
>  sub("^  ", "", "  fghaghf")
[1] "fghaghf"
>

And restarting R I had this message:

Error:10061
R is not in server mode.
Please, start R and/or run the startSocket Server function
available in svSocket package!

Anyone could help me,

--
Massimiliano Tripoli