[R] for cycle with uncontinuous numbers

David Winsemius dwinsemius at comcast.net
Sun Sep 27 18:19:18 CEST 2009


You did not make "test" large enough to accept an index of 50 or any  
of the other numbers in x for that matter. On the first pass through  
that loop you to assigned to the 50 to the 50th element in test. If  
you wanted to assign 50 to the first element of test the you should  
look at the seq_along function:

 > x <- matrix( c(50,100,200,300,900,2343) ,ncol = 1)
 > for (i in seq_along(x) ){
+ test [i] <- x[i]
+ }
 > test
      [,1]
[1,]   50
[2,]  100
[3,]  200
[4,]  300
[5,]  900
[6,] 2343



On Sep 27, 2009, at 11:45 AM, Marcio Resende wrote:

>
> Dear John,
> What I am trying to do is a genetic analysis and the "i" in my cycle  
> are the
> numbers of markers I am testing.
> I know the code is not right, but what i wanted to do was to fill  
> this 6 row
> matrix with the numbers in x
> So basically I wanted the cycle to loop only with the values on x
> (50,100,200,300,900,2343) and not the the rest of the values
> Do you know how to do it?
>
> In the example I wanted my final test matrix to be
> [50
> 100
> 200
> 300
> 900
> 2343]
> dim(test) <- still 6x1

And probably all zeroes, right?

>
> Thanks for the help
>
>
>
>
> jholtman wrote:
>>
>> It is unclear exactly what you are trying to do.  Since your 'test'
>> matrix is only 6 rows in length, then when you extend it by trying to
>> store into 50 for example, it will fill in the rest of the new values
>> with NA.  Can you explain what you think the code is supposed to do?
>> It is doing exactly what you are asking it to do with the script you
>> provided.
>>
>> On Sun, Sep 27, 2009 at 10:09 AM, Marcio Resende
>> <mresendeufv at yahoo.com.br> wrote:
>>>
>>> Hi Tobias, thanks for the help,
>>> the code I am using is quite long, but basically what I tried to  
>>> do was
>>>
>>> test <- matrix(0,6,1)
>>> x <- matrix( c(50,100,200,300,900,2343) ,ncol = 1)
>>> for (i in x){
>>> test [i] <- (i)
>>> }
>>>
>>> but this code returns NA for all the elements which are not x
>>>
>>>
>>>
>>>
>>> Tobias Verbeke-2 wrote:
>>>>
>>>>> Hi nice people,
>>>>
>>>> :-)
>>>>
>>>>> I would like to do a for cycle but i wish it to assume only the  
>>>>> numers
>>>>> 50,
>>>>> 100, 200, 300, 900 and 2343
>>>>> I tried to do something like
>>>>>
>>>>> x <- c(50,100,200,300,900,2343)
>>>>> for (i in x){
>>>>> #.....
>>>>> }
>>>>>
>>>>> But it didn´t work
>>>>
>>>> If you would use a reproducible code example we
>>>> could point out where the error comes from;
>>>> otherwise we can only tell this should work;
>>>> try
>>>>
>>>> x <- c(50,100,200,300,900,2343)
>>>> for (i in x){
>>>>    cat(i^2, "\n")
>>>> }
>>>> # 2500
>>>> # 10000
>>>> # 40000
>>>> # 90000
>>>> # 810000
>>>> # 5489649
>>>>
>>>> HTH,
>>>> Tobias
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/for-cycle-with-uncontinuous-numbers-tp25630831p25633906.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> -- 
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>> ______________________________________________
>> 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.
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/for-cycle-with-uncontinuous-numbers-tp25630831p25634676.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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




More information about the R-help mailing list