IF LOOOP

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

IF LOOOP

matthias worni-2
Hey

This should be a rather simple quesiton for some of you. I want to make
some progress in looping...
I have the vector r, which contains single values --> see below:

r
  [1] 1.1717118 1.1605215 1.1522907 1.1422830 1.1065277 1.1165451 1.1163768
1.1048872 1.0848836 1.0627211
 [11] 1.0300964 1.0296879 1.0308194 1.0518188 1.0657229 1.0685514 1.0914881
1.1042577 1.1039351 1.0880163


I would like to take out simply the value "0.990956" from the vector,
printing out the rest of it.  The code is from the internet but does not
seem to work for my vector. Can't figure out why... Thanks for the help

r <- as.vector(lw)
count=0
for (i in r)  {
  if(i == 0.990956) {
    break
  }
    print(i)
  }


Best
Matthias

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] IF LOOOP

Rolf Turner
On 14/06/17 08:46, matthias worni wrote:

> Hey
>
> This should be a rather simple quesiton for some of you. I want to make
> some progress in looping...
> I have the vector r, which contains single values --> see below:
>
> r
>    [1] 1.1717118 1.1605215 1.1522907 1.1422830 1.1065277 1.1165451 1.1163768
> 1.1048872 1.0848836 1.0627211
>   [11] 1.0300964 1.0296879 1.0308194 1.0518188 1.0657229 1.0685514 1.0914881
> 1.1042577 1.1039351 1.0880163
>
>
> I would like to take out simply the value "0.990956" from the vector,
> printing out the rest of it.  The code is from the internet but does not
> seem to work for my vector. Can't figure out why... Thanks for the help
>
> r <- as.vector(lw)
> count=0
> for (i in r)  {
>    if(i == 0.990956) {
>      break
>    }
>      print(i)
>    }

FAQ 7.31

cheers,

Rolf Turner


--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] IF LOOOP

David Winsemius

> On Jun 13, 2017, at 4:10 PM, Rolf Turner <[hidden email]> wrote:
>
> On 14/06/17 08:46, matthias worni wrote:
>> Hey
>> This should be a rather simple quesiton for some of you. I want to make
>> some progress in looping...
>> I have the vector r, which contains single values --> see below:
>> r
>>   [1] 1.1717118 1.1605215 1.1522907 1.1422830 1.1065277 1.1165451 1.1163768
>> 1.1048872 1.0848836 1.0627211
>>  [11] 1.0300964 1.0296879 1.0308194 1.0518188 1.0657229 1.0685514 1.0914881
>> 1.1042577 1.1039351 1.0880163
>> I would like to take out simply the value "0.990956" from the vector,
>> printing out the rest of it.  The code is from the internet but does not
>> seem to work for my vector.

I'm not sure that the source of this code should be considered a trusted foundation for learning R. You should not be using for-loops to modify vectors in this manner.


>> Can't figure out why... Thanks for the help
>> r <- as.vector(lw)

That's probably not needed.

>> count=0
>> for (i in r)  {
>>   if(i == 0.990956) {
>>     break

I suspect you meant to use:

?'next'   # since `break` completely terminates a for-loop.


The ?'help' page has all the "control structures": `for`, `repeat`, `while` and associated boundaries and terminators

Both `break` and `next` are reserved words (names of functions, control tokens), so using the `?` operator requires quoting.

Also that for-next loop would do _nothing_ to the value of r. Printing would not modify the value of `r`.

You should read:

?Reserved

?'for'  # since `for` is also reserved

?print



>>   }
>>     print(i)
>>   }
>
> FAQ 7.31

After following Rolf's advice ... Try:

r[ all.equal(r, 0.990956) ]

>


> cheers,
>
> Rolf Turner

Further note to matthias:
All caps in Subject is considered poor form, as is posting in HTML.

--

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: IF LOOOP

Jim Lemon-4
In reply to this post by matthias worni-2
Hi Matthias.
The first thing I notice is that the vector r:

r<-c(1.1717118,1.1605215,1.1522907,1.1422830,1.1065277,1.1165451,
 1.1163768,1.1048872,1.0848836,1.0627211,1.0300964,1.0296879,
 1.0308194,1.0518188,1.0657229,1.0685514,1.0914881,1.1042577,
 1.1039351,1.0880163)

doesn't contain the value 0.990956. If it ain't there, you can't
remove it. It has already been suggested that the failure of any
explicit value like 0.990956 to equal its displayed representation is
due to machine precision. I'll guess that you really want to remove a
range of values, something like those less than 1.000000. If so, try
this:

r[r>=1]

Jim

On Wed, Jun 14, 2017 at 6:46 AM, matthias worni <[hidden email]> wrote:

> Hey
>
> This should be a rather simple quesiton for some of you. I want to make
> some progress in looping...
> I have the vector r, which contains single values --> see below:
>
> r
>   [1] 1.1717118 1.1605215 1.1522907 1.1422830 1.1065277 1.1165451 1.1163768
> 1.1048872 1.0848836 1.0627211
>  [11] 1.0300964 1.0296879 1.0308194 1.0518188 1.0657229 1.0685514 1.0914881
> 1.1042577 1.1039351 1.0880163
>
>
> I would like to take out simply the value "0.990956" from the vector,
> printing out the rest of it.  The code is from the internet but does not
> seem to work for my vector. Can't figure out why... Thanks for the help
>
> r <- as.vector(lw)
> count=0
> for (i in r)  {
>   if(i == 0.990956) {
>     break
>   }
>     print(i)
>   }
>
>
> Best
> Matthias
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.