[R] how to do this "the R way"

Patrick Burns pburns at pburns.seanet.com
Sat Apr 4 13:11:29 CEST 2009


You can find a discussion of subscripting
in pretty much any document on R.


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")

onyourmark wrote:
> Yes, That is it!
> Can I just make sure I understand it?
> the second entry in perm[ , ] indicates the column, but you are putting in a
> vector. This will therefore give a vector?? (any suggestions under what
> topic I can read about this?)
> So it is like a loop and each time it is filling in the next entry from TS2
> and therefore choosing that column (and 100th row). A little difficult to
> get my head around it but it works.
>
> Thanks a lot.
>
> onyourmark wrote:
>   
>> WOW. Is it really that compact? I will give it a try. Amazing if true.
>> Thanks.
>>
>> Patrick Burns wrote:
>>     
>>> You need a comma (,) not a dot (.) in your
>>> subscripting of the matrix.  If I get the question
>>> correctly, you want:
>>>
>>> newTS2 <- perm[100, TS2]
>>>
>>>
>>> Patrick Burns
>>> patrick at burns-stat.com
>>> +44 (0)20 8525 0696
>>> http://www.burns-stat.com
>>> (home of "The R Inferno" and "A Guide for the Unwilling S User")
>>>
>>> onyourmark wrote:
>>>       
>>>> Hi. 
>>>>  newTS2 =(perm[100.TS2[1]]. perm[100.TS2[2]]. perm[100.TS2[3]]. ... ,
>>>> perm[100.TS2[2000]])
>>>>
>>>> newTS2 is supposed to be a vector of 2000 values.
>>>> The first value is supposed come from a particular element of the matrix
>>>> called 'perm'.
>>>>
>>>> I thought that 
>>>> perm[100.TS2[1]]
>>>> would be valid code in R to refer to that element.
>>>> It is meant to choose the element from 'perm' in the 100th row and in
>>>> the
>>>> column indicated by
>>>> TS2[1]. In other words, TS2[1] is the first element in the TS2 vector. 
>>>>
>>>> For example, if the first element in the TS2 vector was 5 then I would
>>>> want
>>>> to choose the (100,5) element from perm to be the first entry in 
>>>> newTS2.
>>>>
>>>> Does this make sense? Thanks.
>>>>
>>>> onyourmark wrote:
>>>>   
>>>>         
>>>>> Hi. I am sure there is a better way in R to do this then using a loop
>>>>> but
>>>>> I
>>>>> am new to it and not sure what to do. I think it might be something
>>>>> about
>>>>> using a function as an argument but not sure.
>>>>>
>>>>> I have a 1 x 2000 vector TS2 which has entries from the set {x: x is in
>>>>> Z
>>>>> and 0<x<8} (where Z is the set of Integers).
>>>>>
>>>>> Then I also have a 5050 x 7 matrix called 'perm' whose entries are also
>>>>> from
>>>>> the set {x: x is in Z and 0<x<8}
>>>>>
>>>>> I want to construct the following transformation of TS2 which will
>>>>> still
>>>>> be
>>>>> a vector of size 1 x 2000 and which I will call 'newTS2' such that:
>>>>>
>>>>> newTS2 =(perm[100.TS2[1]]. perm[100.TS2[2]]. perm[100.TS2[3]]. ... ,
>>>>>     
>>>>>           
>>>> Perhaps you can explain what the last line is supposed to do? This is 
>>>> invalid R code and also not a known mathematical notation for me ....
>>>>
>>>> Uwe Ligges
>>>>
>>>>
>>>>   
>>>>         
>>>>> perm[100.TS2[2000]])
>>>>>
>>>>> Is there a nice way to do this without a loop?
>>>>>
>>>>> Thank  you.
>>>>>     
>>>>>           
>>>> ______________________________________________
>>>> 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.
>>>>
>>>>
>>>>
>>>>
>>>>         
>>> ______________________________________________
>>> 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.
>>>
>>>
>>>       
>>     
>
>




More information about the R-help mailing list