[R-sig-teaching] Optimizing loop

Ulises M. Alvarez uma at sophie.unam.mx
Thu Feb 5 18:09:52 CET 2015


On 02/05/2015 02:08 AM, Manel Amado Martí wrote:
> 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!

Hi:

You may take a look at the dplyr library:

https://github.com/hadley/dplyr

If you provide a small, reproducible example, we may provide further help.
-- 
Ulises M. Alvarez
http://sophie.unam.mx/



More information about the R-sig-teaching mailing list