Thanks Phil. That worked.
The apply(..) commands gives me a single 1/0 output for each column which has the query item or not. But I am looking to find the index i from the loop to extract that particular value for plotting I'll try to improve the data.frame part; I was not able to access the rows with data (just started R), so I created individual frames for data Cheers Mohan -----Original Message----- From: Phil Spector [mailto:spec...@stat.berkeley.edu] Sent: Wednesday, March 11, 2009 10:46 PM To: Mohan Singh Subject: Re: [R] R-help: grep in for loop using index - doesn't work Mohan - "i" is the literal character i. i is the variable called i. In this case, you want the variable, i.e. i: p1 <- grep("QueryItem",c1data[i,],value=FALSE,fixed=FALSE) I might also mention that creating a separate data frame for each column of your data is not really a good idea. I believe that you're looking for a solution like apply(data,2,grep,"Query Item") but it's hard to tell. You might want to run that command and see if what you want is in the output. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Wed, 11 Mar 2009, Mohan Singh 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) } > > } > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.