[R] data frame search

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jun 24 13:15:04 CEST 2006


Try this:

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


On 6/24/06, Bart Joosen <bartjoosen at 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



More information about the R-help mailing list