Getting Rid of NaN in ts Object

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting Rid of NaN in ts Object

Lorenzo Isella
Dear All,
I am sure the answer is a one liner, but I am banging my head against
the wall and googling here and there has not helped much.
Consider the following time series

tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
), .Tsp = c(1980, 2015, 1), class = "ts")


where the NaN do *not* occur internally. How can I automatically get
rid of them and adjust the start and end year of the time series
accordingly?
Many thanks

Lorenzo

______________________________________________
[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: Getting Rid of NaN in ts Object

Jim Lemon-4
Hi Lorenzo,
Maybe:

tt<-tt[!is.nan(tt)]

Jim


On Fri, May 27, 2016 at 8:14 PM, Lorenzo Isella
<[hidden email]> wrote:

> Dear All,
> I am sure the answer is a one liner, but I am banging my head against
> the wall and googling here and there has not helped much.
> Consider the following time series
>
> tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
> NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
> 1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
> 1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
> ), .Tsp = c(1980, 2015, 1), class = "ts")
>
>
> where the NaN do *not* occur internally. How can I automatically get
> rid of them and adjust the start and end year of the time series
> accordingly?
> Many thanks
>
> Lorenzo
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting Rid of NaN in ts Object

PIKAL Petr
Hm

> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Jim Lemon
> Sent: Friday, May 27, 2016 12:46 PM
> To: Lorenzo Isella <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] Getting Rid of NaN in ts Object
>
> Hi Lorenzo,
> Maybe:
>
> tt<-tt[!is.nan(tt)]

This gives you tt object as numeric vector. If you want to retain ts properties you can use window, but in this case you have to know in which year you want to start

e.g.
tt <- window(tt, 1998, 2015)

Regards
Petr


>
> Jim
>
>
> On Fri, May 27, 2016 at 8:14 PM, Lorenzo Isella <[hidden email]>
> wrote:
> > Dear All,
> > I am sure the answer is a one liner, but I am banging my head against
> > the wall and googling here and there has not helped much.
> > Consider the following time series
> >
> > tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
> NaN,
> > NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12, 1182.85,
> > 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13, 1018.18, 920.62,
> > 865.99, 856.29, 841.58, 857.7, 852.71, 890.76 ), .Tsp = c(1980, 2015,
> > 1), class = "ts")
> >
> >
> > where the NaN do *not* occur internally. How can I automatically get
> > rid of them and adjust the start and end year of the time series
> > accordingly?
> > Many thanks
> >
> > Lorenzo
> >
> > ______________________________________________
> > [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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
______________________________________________
[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: Getting Rid of NaN in ts Object

Lorenzo Isella
In reply to this post by Jim Lemon-4
On Fri, May 27, 2016 at 08:46:20PM +1000, Jim Lemon wrote:
>Hi Lorenzo,
>Maybe:
>
>tt<-tt[!is.nan(tt)]
>
>Jim


Not really.

> tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
+ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
+ 1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
+ 1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
+ ), .Tsp = c(1980, 2015, 1), class = "ts")
>
> tt2<-tt[!is.nan(tt)]
> class(tt)
[1] "ts"
> class(tt2)
[1] "numeric"


and I lose all the info about the years.

Lorenzo







>
>
>On Fri, May 27, 2016 at 8:14 PM, Lorenzo Isella
><[hidden email]> wrote:
>> Dear All,
>> I am sure the answer is a one liner, but I am banging my head against
>> the wall and googling here and there has not helped much.
>> Consider the following time series
>>
>> tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
>> NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
>> 1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
>> 1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
>> ), .Tsp = c(1980, 2015, 1), class = "ts")
>>
>>
>> where the NaN do *not* occur internally. How can I automatically get
>> rid of them and adjust the start and end year of the time series
>> accordingly?
>> Many thanks
>>
>> Lorenzo
>>
>> ______________________________________________
>> [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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting Rid of NaN in ts Object

Christian Brandstätter
Hi Lorenzo,

Try:

tt[is.nan(tt)] <- NA
tt <- na.omit(tt)

Best,

Christian


Am 27.05.2016 um 13:38 schrieb Lorenzo Isella:

> On Fri, May 27, 2016 at 08:46:20PM +1000, Jim Lemon wrote:
>> Hi Lorenzo,
>> Maybe:
>>
>> tt<-tt[!is.nan(tt)]
>>
>> Jim
>
>
> Not really.
>
>> tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
> + NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
> + 1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
> + 1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
> + ), .Tsp = c(1980, 2015, 1), class = "ts")
>>
>> tt2<-tt[!is.nan(tt)]
>> class(tt)
> [1] "ts"
>> class(tt2)
> [1] "numeric"
>
>
> and I lose all the info about the years.
>
> Lorenzo
>
>
>
>
>
>
>
>>
>>
>> On Fri, May 27, 2016 at 8:14 PM, Lorenzo Isella
>> <[hidden email]> wrote:
>>> Dear All,
>>> I am sure the answer is a one liner, but I am banging my head against
>>> the wall and googling here and there has not helped much.
>>> Consider the following time series
>>>
>>> tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
>>> NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
>>> 1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
>>> 1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
>>> ), .Tsp = c(1980, 2015, 1), class = "ts")
>>>
>>>
>>> where the NaN do *not* occur internally. How can I automatically get
>>> rid of them and adjust the start and end year of the time series
>>> accordingly?
>>> Many thanks
>>>
>>> Lorenzo
>>>
>>> ______________________________________________
>>> [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.

______________________________________________
[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: Getting Rid of NaN in ts Object

Lorenzo Isella
Perfect!
Exactly what I was looking for.
Thanks

Lorenzo

On Fri, May 27, 2016 at 01:50:03PM +0200, Christian Brandstätter wrote:

>Hi Lorenzo,
>
>Try:
>
>tt[is.nan(tt)] <- NA
>tt <- na.omit(tt)
>
>Best,
>
>Christian
>
>
>Am 27.05.2016 um 13:38 schrieb Lorenzo Isella:
>>On Fri, May 27, 2016 at 08:46:20PM +1000, Jim Lemon wrote:
>>>Hi Lorenzo,
>>>Maybe:
>>>
>>>tt<-tt[!is.nan(tt)]
>>>
>>>Jim
>>
>>
>>Not really.
>>
>>>tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
>>+ NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
>>+ 1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
>>+ 1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
>>+ ), .Tsp = c(1980, 2015, 1), class = "ts")
>>>
>>>tt2<-tt[!is.nan(tt)]
>>>class(tt)
>>[1] "ts"
>>>class(tt2)
>>[1] "numeric"
>>
>>
>>and I lose all the info about the years.
>>
>>Lorenzo
>>
>>
>>
>>
>>
>>
>>
>>>
>>>
>>>On Fri, May 27, 2016 at 8:14 PM, Lorenzo Isella
>>><[hidden email]> wrote:
>>>>Dear All,
>>>>I am sure the answer is a one liner, but I am banging my head against
>>>>the wall and googling here and there has not helped much.
>>>>Consider the following time series
>>>>
>>>>tt<-structure(c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
>>>>NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1133.09, 1155.77, 1179.12,
>>>>1182.85, 1133.43, 1103.36, 1081.19, 1058.55, 1056.95, 1059.13,
>>>>1018.18, 920.62, 865.99, 856.29, 841.58, 857.7, 852.71, 890.76
>>>>), .Tsp = c(1980, 2015, 1), class = "ts")
>>>>
>>>>
>>>>where the NaN do *not* occur internally. How can I automatically get
>>>>rid of them and adjust the start and end year of the time series
>>>>accordingly?
>>>>Many thanks
>>>>
>>>>Lorenzo
>>>>
>>>>______________________________________________
>>>>[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.
>

______________________________________________
[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: Getting Rid of NaN in ts Object

Martin Maechler

> Perfect!
> Exactly what I was looking for.
> Thanks

> Lorenzo

> On Fri, May 27, 2016 at 01:50:03PM +0200, Christian Brandstätter wrote:
>> Hi Lorenzo,
>>
>> Try:
>>
>> tt[is.nan(tt)] <- NA
>> tt <- na.omit(tt)
>>

or simply  na.omit(tt)

as it omits both NA and NaN (and *does* keep the 'ts' properties
as you have noted).

Martin

>> Best,
>>
>> Christian
>>

______________________________________________
[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.