[R] Positions in datasets

Anders Falk Anders.Falk at vbsg.slu.se
Sun Dec 27 18:11:32 CET 2009


Could someone help me understand this

Basically I want to know the position of certain numbers in large output
data sets. First consider the following simple example where we get the
postions of ones (1) in the vector q.

> q <- c(5,1,1,3,1,1,1,1)
> q
[1] 5 1 1 3 1 1 1 1
> for (i in 1:length(q)) if(q[i]==1) print(i)
[1] 2
[1] 3
[1] 5
[1] 6
[1] 7
[1] 8

Well done!


But now consider the following case where the input consists of different
combinations of the three numbers 2, 3 and 7. They are put into a function
1/x +1/y +1/z +1/(x*y*z). All different combinations will yield the same
result namely precisely 1, which is also clearly seen in the output.
However when I try to get information on the position of the ones in the
output, there are only two that are recognized, although all six items in
the output are indeed ones.

> x <- c(2,2,3,3,7,7); y <- c(3,7,2,7,2,3); z <- c(7,3,7,2,3,2)
> data.frame(x,y,z)
  x y z
1 2 3 7
2 2 7 3
3 3 2 7
4 3 7 2
5 7 2 3
6 7 3 2
> p <- numeric(length(x))
> for (i in 1:length(x)) p[i] <- ((1/x[i]) + (1/y[i]) + (1/z[i]) +
(1/(x[i]*y[i]*z[i])))
> p
[1] 1 1 1 1 1 1
> for (i in 1:length(p)) if(p[i]==1) print(i)
[1] 4
[1] 6


I suppose there must exist some better way of accessing the position of
certain numbers in a large data output. So apart from getting advice on
that I would certainly also like to understand why the above only seems to
work in some cases.

Anders B Falk PhD
Uppsala Sweden




More information about the R-help mailing list