Luca Meyer
2018-Apr-24 07:30 UTC
[R] How to visualise what code is processed within a for loop
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]]
Bob O'Hara
2018-Apr-24 08:19 UTC
[R] How to visualise what code is processed within a for loop
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 at 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 at 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
Luca Meyer
2018-Apr-24 10:09 UTC
[R] How to visualise what code is processed within a for loop
Hi Bob, Thank you for your suggestion. Actually d0 is a dataframe, does that change something in the code you propose? Kind regards, Luca 2018-04-24 10:19 GMT+02:00 Bob O'Hara <rni.boh at gmail.com>:> 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 at 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 at 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 >[[alternative HTML version deleted]]
MacQueen, Don
2018-Apr-25 21:03 UTC
[R] How to visualise what code is processed within a for loop
Your code doesn't make sense to me in a couple of ways. Inside the loop, the first line assigns a value to an object named "t". Then, the second line does the same thing, assigns a value to an object named "t". The value of the object named "t" after the second line will be the output of the ifelse() expression, whatever that is. This has the effect of making the first line irrelevant. Whatever value t has after the first line is replaced by whatever it gets from the second line. It looks like the first line inside the loop is constructing the name of a data frame column, and storing that name as a character string. However, the second line doesn't use that name at all. If your goal is to update the contents of a column, you need to assign something to that column in the next line. Instead you assign it to the object named "t". What you're looking for will be more along the lines of this: for (i in 1:10){ nm <- paste0("V", i) d0[[nm]] <- ifelse( regexpr(d1[i,1], d0$X0) > 0, 1, 0) } This may not a complete solution, since I have no idea what the contents or structure of d1 are, or what the regexpr() is expected to return. And notice the use of double brackets, [[ and ]]. This is one way to reference a column of a data frame when you have the column's name stored in a variable. Another way is d0[ , nm] A couple of additional comments: "t" is a poor choice of object name, because it is one of R's built-in functions (immediately after starting a fresh session of R, with nothing left over from any previous session, type help("r") and see what you get). ifelse() is intended for use on vectors, not scalars, and it looks like maybe you're using it on a scalar (can't be sure about this, though) For example, ifelse() is designed for this kind of usage:> ifelse( c(TRUE, FALSE, TRUE) , 1:3, 11:13)[1] 1 12 3 Although it works ok for these> ifelse(TRUE, 3, 4)[1] 3> ifelse(FALSE, 3, 4)[1] 4 They are not really what it is intended for. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 ?On 4/24/18, 12:30 AM, "R-help on behalf of Luca Meyer" <r-help-bounces at r-project.org on behalf of lucam1968 at 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 at 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.
Luca Meyer
2018-Apr-28 19:45 UTC
[R] How to visualise what code is processed within a for loop
Thanks Don, for (i in 1:10){ nm <- paste0("V", i) d0[[nm]] <- ifelse( regexpr(d1[i,1], d0$X0) > 0, 1, 0) } is exaclty what I needed. Best regards, Luca 2018-04-25 23:03 GMT+02:00 MacQueen, Don <macqueen1 at llnl.gov>:> Your code doesn't make sense to me in a couple of ways. > > Inside the loop, the first line assigns a value to an object named "t". > Then, the second line does the same thing, assigns a value to an object > named "t". > > The value of the object named "t" after the second line will be the output > of the ifelse() expression, whatever that is. This has the effect of making > the first line irrelevant. Whatever value t has after the first line is > replaced by whatever it gets from the second line. > > It looks like the first line inside the loop is constructing the name of a > data frame column, and storing that name as a character string. However, > the second line doesn't use that name at all. If your goal is to update the > contents of a column, you need to assign something to that column in the > next line. Instead you assign it to the object named "t". > > What you're looking for will be more along the lines of this: > > for (i in 1:10){ > nm <- paste0("V", i) > d0[[nm]] <- ifelse( regexpr(d1[i,1], d0$X0) > 0, 1, 0) > } > > This may not a complete solution, since I have no idea what the contents > or structure of d1 are, or what the regexpr() is expected to return. > > And notice the use of double brackets, [[ and ]]. This is one way to > reference a column of a data frame when you have the column's name stored > in a variable. Another way is d0[ , nm] > > > A couple of additional comments: > > "t" is a poor choice of object name, because it is one of R's built-in > functions (immediately after starting a fresh session of R, with nothing > left over from any previous session, type help("r") and see what you get). > > ifelse() is intended for use on vectors, not scalars, and it looks like > maybe you're using it on a scalar (can't be sure about this, though) > > For example, ifelse() is designed for this kind of usage: > > ifelse( c(TRUE, FALSE, TRUE) , 1:3, 11:13) > [1] 1 12 3 > > Although it works ok for these > > ifelse(TRUE, 3, 4) > [1] 3 > > ifelse(FALSE, 3, 4) > [1] 4 > They are not really what it is intended for. > > -- > Don MacQueen > Lawrence Livermore National Laboratory > 7000 East Ave., L-627 > Livermore, CA 94550 > 925-423-1062 > Lab cell 925-724-7509 > > > ?On 4/24/18, 12:30 AM, "R-help on behalf of Luca Meyer" < > r-help-bounces at r-project.org on behalf of lucam1968 at 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 at 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. > > >[[alternative HTML version deleted]]
Seemingly Similar Threads
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop