[R] How to visualise what code is processed within a for loop

Bob O'Hara rn|@boh @end|ng |rom gm@||@com
Tue Apr 24 10:19:45 CEST 2018


The loop never assigns anything to d0, only t. The first line makes t
a character string "d0$V1" (or "d0$V2" etc.). The second line assigns
either 0 or 1 to t.

Looking at this, I don't think you've got into the R psychology (bad
news if you want to use R, good news in many other ways). I assume d0
is a list, so could you put the V's into a vector, and then just use
this:

d0$V <- sapply(d1[1:10,1], grepl, d0$X0)

(I haven't checked it, but it looks,like it will do the trick. It
returns a logical vector, so if you need integers, then use an
as.numeric() around the right hand side. Or hope that R does type
conversion for you when you need it)

HTH

Bob

On 24 April 2018 at 09:30, Luca Meyer <lucam1968 using gmail.com> wrote:
> Hi,
>
> I am trying to debug the following code:
>
> for (i in 1:10){
>   t <- paste("d0$V",i,sep="")
>   t <- ifelse(regexpr(d1[i,1],d0$X0)>0,1,0)
> }
>
> and I would like to see what code is actually processing R, how can I do
> that?
>
> More to the point, I am trying to update my variables d0$V1 to d0$V10
> according to the presence or absence of some text (contained in the file
> d1) within the d0$X0 variable.
>
> The code seem to run ok, if I add print(table(t)) within the loop I can see
> that the ifelse procedure is working and to some cases within the d0$V1 to
> d0$V10 variable range a 1 is assigned. But when checking my d0$V1 to d0$V10
> after the for loop they are all still equal to zero...
>
> Thanks,
>
> Luca
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



-- 
Bob O'Hara
NOTE NEW ADDRESS!!!
Institutt for matematiske fag
NTNU
7491 Trondheim
Norway

Mobile: +49 1515 888 5440
Journal of Negative Results - EEB: www.jnr-eeb.org




More information about the R-help mailing list