Re: [R] data frame search

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 24 Jun 2006 - 21:15:04 EST

Try this:

which(DF1 == 4, arr.ind = TRUE)

On 6/24/06, Bart Joosen <bartjoosen@hotmail.com> wrote:
> Hi,
>
> I want to make a data frame which contains the positions of some searched values in another data frame.
>
> Like:
> Dataframe 1:
>
> 1 2 3 4 1 2 3 4
> 2 3 4 1 2 3 4 2
> 4 1 2 3 2 3 4 1
>
> Let's say I searched on "4", then Dataframe 2 should contain:
> x y
> 1 4
> 1 8
> 2 3
> 2 7
> 3 1
> 3 7
>
> I have written a routine, but it seems to me that it isn't that perfect:
> x<- 0
>
> y<- 0
>
> for (j in 1:ncol(df)) {
>
> for (i in 1:nrow(df)) {
>
> if (df[i,j] == 3) {
>
> x <- c(x,i)
>
> y <- c(y,j)
>
> }
>
> }
>
> }
>
> df2 <- data.frame(x,y)
>
> df2 <- df2[-1,]
>
> Can someone come up with an elegant/faster solution, because the ultimate goal of this routine is to analyze an jpg image.
>
> Kind regards
>
> Bart



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jun 24 21:29:15 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 25 Jun 2006 - 06:11:55 EST.

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