[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
}
I had this result:
> 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,
Thanks in advance
--
Massimiliano Tripoli
Collaboratore T.E.R. scado il 31/12/2014
ISTAT - DCCN - Direzione Centrale della Contabilità Nazionale
U.O. Conti trimestrali delle amministrazioni pubbliche e conti della sanità - FIP/E
Via Depretis, 74/B 00184 Roma
Tel. 06.4673.3132
E-mail: mtripoli at istat.it
More information about the R-help
mailing list