[R] R-help: grep in for loop using index - doesn't work

Mohan Singh Mohan.Singh at ucd.ie
Wed Mar 11 23:29:51 CET 2009


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) }
}




More information about the R-help mailing list