[R-sig-teaching] Optimizing loop

Manel Amado Martí amado at cambrasabadell.org
Thu Feb 5 09:08:21 CET 2015


I'm processing a table database. To do that, I put it in a dataframe, and then I do the data processing (normalization of some fields). I'm used to program in C, and some R's facilities are not so natural to me, please, excuse me if the question is for "dummies".
In the processing, I want to substitute some field's value depending on the previous content. For example, if field starts with a digit instead of an alpha character, the entire field from the actual row, I'll replace it with "SOLPD". I'm sure that would be another way (maybe through some apply function), but I can't figure how to do.
The code that I'm using now, is:
for( i in 1:nrow(dataframe2)) {
        if(is.na(dataframe2[i,"NIF/NIE"])==FALSE){
                if(str_locate(dataframe2[i,"NIF/NIE"],"\\d")[1]<2){
                        sprintf("elimina NIF autònom: % i\n",i)
                        dataframe2[i,"NIF"]<-"SLOPD"}
                }
        }
}

Thank you for your attention!




Manel Amado i Martí
Cap d'Assessoria de Comerç Interior
amado at cambrasabadell.org<mailto:amado at cambrasabadell.org>
Tel. 93 745 12 63 · Fax 93 745 12 64    [http://www.cambrasabadell.org/Ficheros/mails/Plantilles/facebook.png] <https://www.facebook.com/cambrasabadell>   [http://www.cambrasabadell.org/Ficheros/mails/Plantilles/Twitter.png] <https://twitter.com/CambraSabadell>   [http://www.cambrasabadell.org/Ficheros/mails/Plantilles/LinkedIn.png] <http://www.linkedin.com/company/cambra-de-comer-de-sabadell?trk=company_name>
Av. Francesc Macià, 35 · 08206 Sabadell
Apt. corr. 119 · www.cambrasabadell.org<http://www.cambrasabadell.org>

[http://www.cambrasabadell.org/Ficheros/mails/Plantilles/peu.png]
Aquest missatge pot contenir informació confidencial o sotmesa a secret professional, la divulgació de la qual està prohibida per la llei. Si no sou el destinatari del missatge, si us plau, esborreu-lo i comuniqueu-nos-ho immediatament, no el reenvieu ni en copieu el contingut. Si la vostra empresa no permet rebre missatges d'aquesta mena, si us plau, feu-nos-ho saber immediatament.
Este mensaje puede contener información confidencial o sometida a secreto profesional, cuya divulgación está prohibida por la ley. Si no es usted el destinatario del mensaje, le rogamos que lo borre y nos lo notifique inmediatamente; no lo reenvíe ni copie su contenido. Si su empresa no permite la recepción de mensajes de este tipo, por favor háganoslo saber inmediatamente.
This message may contain confidential information that i...{{dropped:11}}



More information about the R-sig-teaching mailing list