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

From: Mohan Singh <Mohan.Singh_at_ucd.ie>
Date: Wed, 11 Mar 2009 23:02:45 +0000

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:spector_at_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.

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_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.
>



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. Received on Wed 11 Mar 2009 - 22:06:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 11 Mar 2009 - 23:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive