[R] sub function problem

Massimiliano Tripoli mtripoli at istat.it
Mon Mar 3 17:39:03 CET 2014


Hi Petr,
thanks for your response.
You're right. I counted incorrectly the numbers of spaces at the beginning of the sub function directly called as you can see at the end.
If the string has a length more than 10 it works correctly returning the same string of input except if there are some spaces at the
beginning, that correctly it reduces to one as it expected.
But what about the error 10061 that I usually encountered ? In this case I used Tinn-R (I know it's not a Tinn-R forum sorry). Is it correlated with it.

Thanks



----- Messaggio originale -----
Da: "PIKAL Petr" <petr.pikal at precheza.cz>
A: "Massimiliano Tripoli" <mtripoli at istat.it>, r-help at r-project.org
Inviato: Lunedì, 3 marzo 2014 10:36:02
Oggetto: RE: [R] sub function problem

Hi

Everything works for me as expected. Nothing happens R starts as usual.

You can shorten your function

corregge2 <- function(stringa){
nc <- nchar(stringa)
     stringa <- sub("^ ", "", stringa)
     stringa <- sub(" +$", "", stringa)
if (nc <9) stringa <- NA
if (nc == 9) stringa <- paste("0",stringa,sep="")
if (nc == 10) stringa <- paste("00",stringa,sep="")
stringa
}

What if your string has length more than 10?

Regards
Petr

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Massimiliano Tripoli
> Sent: Friday, February 28, 2014 5:44 PM
> To: r-help at r-project.org
> Subject: [R] sub function problem
>
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.

-- 
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