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

baptiste auguie ba208 at exeter.ac.uk
Thu Mar 12 16:25:39 CET 2009


Hi,

your example is quite messy (neither reproducible or minimal). I think  
you could try the following,


> mdf <- data.frame(1:3)
> names(mdf) <- "147"
>
> i <- 147
>
> mdf[ as.character(i) ]


Hope this helps,

baptiste


On 11 Mar 2009, at 22:34, 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list