[R] R-help: grep in for loop using index - doesn't work
jim holtman
jholtman at gmail.com
Thu Mar 12 03:19:02 CET 2009
I think you have to convert the loop value to character; it is not
recognized inside the quotes:
#doesn't work, but now it should
i<-147
p1 <- grep("QueryItem",c1data[as.character(i),],value=FALSE,fixed=FALSE)
print(p1) # prints 0
Same thing goes for the 'for' loop:
for(i in 137:270) {
print(i)
p <- grep("QueryItem",c1data[as.character(i),],value=FALSE,fixed=FALSE)
p2 <- p
print(p2)
if(length(p)>0) { print(p) }
}
On Wed, Mar 11, 2009 at 6:29 PM, Mohan Singh <Mohan.Singh at ucd.ie> wrote:
> Hi everyone
>
> I am trying to use grep in a for loop to compare a string value. It works
> if I use
> the actual index value but when I use the for loop index, it doesn't work.
> Any suggestions plz.
>
> Here is the code:
>
> data <- read.table(file="Sigmoid.csv", head=FALSE, sep=",");
>
> c1 <- data$V1
> c2 <- data$V2
> c3 <- data$V3
>
> c1data <- data.frame(c1);
> c2data <- data.frame(c2);
> c3data <- data.frame(c3);
>
> #this works
> p <- grep("QueryItem",c1data["147",],value=FALSE,fixed=FALSE)
> print(p) # print 1
>
> #doesn't work
> i<-147
> p1 <- grep("QueryItem",c1data["i",],value=FALSE,fixed=FALSE)
> print(p1) # prints 0
>
> for(i in 137:270) {
> print(i)
> p <- grep("QueryItem",c1data["i",],value=FALSE,fixed=FALSE)
> p2 <- p
> print(p2)
> if(length(p)>0) { print(p) }
> }
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list