[R] Positions in datasets

Anders Falk Anders.Falk at vbsg.slu.se
Sun Dec 27 21:42:58 CET 2009


Thank you both for your excellent help.

Best Regards, Anders Falk


>
>
> David Winsemius wrote:
>>
>> On Dec 27, 2009, at 12:11 PM, Anders Falk wrote:
>>
>>> 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.
>>
>> "data output" is a bit vague, but if you are talking about vectors then:
>>
>> ?which
>>
>>> So apart from getting advice on
>>> that I would certainly also like to understand why the above only
>>> seems to
>>> work in some cases.
>>
>> It's a FAQ, 7.31:
>> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>>
>>
>>  > for (i in 1:length(p)) if(all.equal(p[i],1)) print(i)
>> [1] 1
>> [1] 2
>> [1] 3
>> [1] 4
>> [1] 5
>> [1] 6
>
>
>
> Completely right, but just for completeness let me add that you do never
> want to say 1:length(p) in your code, because it might happen that p
> becomes empty and your loop will iterate over 1:0, hence better always
> (then you'll never run into that problem) use seq_along as in:
>
> for (i in seq_along(p)) if(all.equal(p[i],1)) print(i)
>
> Best,
> Uwe Ligges
>
>
>>> Anders B Falk PhD
>>> Uppsala Sweden
>>>
>>> ______________________________________________
>>> 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.
>>
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>>
>> ______________________________________________
>> 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.
>


Anders Falk

Mobil: 0707-913910
email: anders.falk at vbsg.slu.se




More information about the R-help mailing list