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 <macque...@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-boun...@r-project.org on behalf of lucam1...@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@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]] ______________________________________________ R-help@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.