Re: [R] Manipulating two large dataset differing by date and time

2018-01-21 Thread Jim Lemon
Hi Ogbos,
You can just use ISOdate. If you pass more values, it will process them:

ISOdate(2018,01,22)
[1] "2018-01-22 12:00:00 GMT"
> ISOdate(2018,01,22,18,17)
[1] "2018-01-22 18:17:00 GMT"

Add something like:

if(is.null(data$hour),data$hour<-12

then pass data$hour as it will default to the same value as if you
hadn't passed it.

Jim

On Mon, Jan 22, 2018 at 6:01 PM, Ogbos Okike  wrote:
> Dear Members,
>
> Compliments of the Season!!
>
>
> Below is a part of a code I use for Fourier analysis of signals. The code
> handles data with the format 05 01 018628 (year, month, day and count)
>   05 01 028589 (year, month, day and count)
> The sample data is attached as 2005daily.txt.
>
> I would like to adapt the code to handle data of the form:
> 05 01 01 004009 (year, month, day, hour and count)
> 05 01 01 013969 (year, month, day, hour and count)
>
> The sample is also attached as 2005hourly.txt.
>
> Thank you very much for your kind inputs.
>
> Ogbos
>
>
>
>
> data <- read.table("2005daily.txt", col.names = c("year", "month", "day",
> "counts"))
>
> new.century <- data$year < 50
>
> data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
>
> data$date <- as.Date(ISOdate(data$year, data$month, data$day))
> x1 = data$date
>  y = data$counts
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


[R] Manipulating two large dataset differing by date and time

2018-01-21 Thread Ogbos Okike
Dear Members,

Compliments of the Season!!


Below is a part of a code I use for Fourier analysis of signals. The code
handles data with the format 05 01 018628 (year, month, day and count)
  05 01 028589 (year, month, day and count)
The sample data is attached as 2005daily.txt.

I would like to adapt the code to handle data of the form:
05 01 01 004009 (year, month, day, hour and count)
05 01 01 013969 (year, month, day, hour and count)

The sample is also attached as 2005hourly.txt.

Thank you very much for your kind inputs.

Ogbos




data <- read.table("2005daily.txt", col.names = c("year", "month", "day",
"counts"))

new.century <- data$year < 50

data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)

data$date <- as.Date(ISOdate(data$year, data$month, data$day))
x1 = data$date
 y = data$counts
__
R-help@r-project.org 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.


Re: [R] Split charts with ggplot2, tidyquant

2018-01-21 Thread Eric Berger
Hi Charlie and Bert,
Thank you both for the suggestions and pointers. I will look into them.

FYI I repeatedly refer to tidyquant because that package refers to itself as
"tidyquant: Tidy Quantitative Financial Analysis" and I am hoping to get the
attention of someone who is involved in the tidyquant package. The type of split
chart I am interested in is standard / prevalent in financial charting,
e.g. the charts on https://www.bloomberg.com/markets/stocks all have
an 'Indicators' button which allows you add, say, a volume chart as a
subchart below the main part of the chart.

Thanks again,
Eric



On Sun, Jan 21, 2018 at 6:45 PM, Charlie Redmon  wrote:
> Thanks for the reminder about lattice! I did some searching and there's a
> good example of manipulating the size of subplots using the `position`
> argument (see pp. 202-203 in the Trellis Users Guide:
> http://ml.stat.purdue.edu/stat695t/writings/Trellis.User.pdf). This is not
> within the paneling environment with the headers like in other trellis plots
> though, so you'll have to do a bit more digging to see how to get that to
> work if you need those headers.
>
>
> Best,
>
> Charlie
>
>
> On 01/20/2018 03:17 PM, Bert Gunter wrote:
>>
>> That (the need for base graphics) is false. It certainly **can** be done
>> in base graphics -- see ?layout for a perhaps more straightforward way to do
>> it along the lines you suggest.
>>
>> However both lattice and ggplot are based on grid graphics, which has a
>> similar but slightly more flexible ?grid.layout function which would allow
>> one to size and place subsequent ggplot or lattice graphs in an arbitrary
>> layout as you have described (iiuc) for the base graphics case.
>>
>> Perhaps even simpler would be to use the "position" argument of the
>> print.trellis() function to locate trellis plots. Maybe ggplot() has
>> something similar.
>>
>> In any case, the underlying grid graphics functionality allows **much**
>> greater fine control of graphical elements (including rotation, for example)
>> -- at the cost of greater complexity. I would agree that doing it from
>> scratch using base grid functions is most likely overkill here, though. But
>> it's there.
>>
>> IMHO only, the base graphics system was great in its time, but its time
>> has passed. Grid graphics is much more powerful because it is objects based
>> -- that is, grid graphs are objects that can be saved, modified, and even
>> interacted with in flexible ways. Lattice and ggplot incarnations take
>> advantage of this, giving them more power and flexibility than the base
>> graphics capabilities can muster.
>>
>> I repeat -- IMHO only! Feel free to disagree. I don't want to start any
>> flame wars here.
>>
>> Cheers,
>> Bert
>>
>>
>>
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along and
>> sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>> On Sat, Jan 20, 2018 at 12:19 PM, Charlie Redmon > > wrote:
>>
>> For this kind of control you will probably need to move to base
>> graphics
>> and utilize the `fig` argument in par(), in which case you would
>> want to
>> run the plot() command twice: once with your first outcome and
>> once with
>> your second, changing the par() settings before each one to
>> control the
>> size.
>>
>>
>> On 01/19/2018 01:39 PM, Eric Berger wrote:
>> > Hi Charlie,
>> > Thanks. This is helpful. As mentioned in my original question, I
>> want
>> > to be able to plot a few such charts on the same page,
>> > say a 2 x 2 grid with such a chart for each of 4 different stocks.
>> > Using your solution I accomplished this by making
>> > a list pLst of your ggplots and then calling cowplot::plot_grid(
>> > plotlist=pLst, nrow=2, ncol=2 )  That worked fine.
>> >
>> > The one issue  I have is that in the ggplot you suggest, the
>> price and
>> > volume facets are the same size. I would like them to be
>> different sizes
>> > (e.g. the volume facet at the bottom is generally shown smaller than
>> > the facet above it in these types of charts.)
>> >
>> > I tried to find out how to do it but didn't succeed. I found a
>> couple
>> > of relevant discussions (including Hadley writing that he did not
>> > think it was a useful feature. :-()
>> >
>> > https://github.com/tidyverse/ggplot2/issues/566
>> 
>> >
>> > and an ancient one where someone seems to have been able to get a
>> > heights parameter working in a call to facet_grid but it did not
>> work
>> > for me.
>> >
>>
>> https://kohske.wordpress.com/2010/12/25/adjusting-the-relative-space-of-a-facet-grid/
>>
>> 
>> >
>> > Thanks again,
>> > Eric
>> >
>> > p.

Re: [R] Split charts with ggplot2, tidyquant

2018-01-21 Thread Charlie Redmon
Thanks for the reminder about lattice! I did some searching and there's 
a good example of manipulating the size of subplots using the `position` 
argument (see pp. 202-203 in the Trellis Users Guide: 
http://ml.stat.purdue.edu/stat695t/writings/Trellis.User.pdf). This is 
not within the paneling environment with the headers like in other 
trellis plots though, so you'll have to do a bit more digging to see how 
to get that to work if you need those headers.



Best,

Charlie


On 01/20/2018 03:17 PM, Bert Gunter wrote:
That (the need for base graphics) is false. It certainly **can** be 
done in base graphics -- see ?layout for a perhaps more 
straightforward way to do it along the lines you suggest.


However both lattice and ggplot are based on grid graphics, which has 
a similar but slightly more flexible ?grid.layout function which would 
allow one to size and place subsequent ggplot or lattice graphs in an 
arbitrary layout as you have described (iiuc) for the base graphics case.


Perhaps even simpler would be to use the "position" argument of the 
print.trellis() function to locate trellis plots. Maybe ggplot() has 
something similar.


In any case, the underlying grid graphics functionality allows 
**much** greater fine control of graphical elements (including 
rotation, for example) -- at the cost of greater complexity. I would 
agree that doing it from scratch using base grid functions is most 
likely overkill here, though. But it's there.


IMHO only, the base graphics system was great in its time, but its 
time has passed. Grid graphics is much more powerful because it is 
objects based -- that is, grid graphs are objects that can be saved, 
modified, and even interacted with in flexible ways. Lattice and 
ggplot incarnations take advantage of this, giving them more power and 
flexibility than the base graphics capabilities can muster.


I repeat -- IMHO only! Feel free to disagree. I don't want to start 
any flame wars here.


Cheers,
Bert





Bert Gunter

"The trouble with having an open mind is that people keep coming along 
and sticking things into it."

-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, Jan 20, 2018 at 12:19 PM, Charlie Redmon > wrote:


For this kind of control you will probably need to move to base
graphics
and utilize the `fig` argument in par(), in which case you would
want to
run the plot() command twice: once with your first outcome and
once with
your second, changing the par() settings before each one to
control the
size.


On 01/19/2018 01:39 PM, Eric Berger wrote:
> Hi Charlie,
> Thanks. This is helpful. As mentioned in my original question, I
want
> to be able to plot a few such charts on the same page,
> say a 2 x 2 grid with such a chart for each of 4 different stocks.
> Using your solution I accomplished this by making
> a list pLst of your ggplots and then calling cowplot::plot_grid(
> plotlist=pLst, nrow=2, ncol=2 )  That worked fine.
>
> The one issue  I have is that in the ggplot you suggest, the
price and
> volume facets are the same size. I would like them to be
different sizes
> (e.g. the volume facet at the bottom is generally shown smaller than
> the facet above it in these types of charts.)
>
> I tried to find out how to do it but didn't succeed. I found a
couple
> of relevant discussions (including Hadley writing that he did not
> think it was a useful feature. :-()
>
> https://github.com/tidyverse/ggplot2/issues/566

>
> and an ancient one where someone seems to have been able to get a
> heights parameter working in a call to facet_grid but it did not
work
> for me.
>

https://kohske.wordpress.com/2010/12/25/adjusting-the-relative-space-of-a-facet-grid/


>
> Thanks again,
> Eric
>
> p.s. Joshua thanks for your suggestions, but I was hoping for a
ggplot
> solution.
>
>
> On Fri, Jan 19, 2018 at 6:33 PM, Charlie Redmon
mailto:redm...@gmail.com>
> >> wrote:
>
>     So the general strategy for getting these into separate
panels in
>     ggplot is to have a single variable that will be your
response and
>     a factor variable that indexes which original variable it came
>     from. This can be accomplished in many ways, but the way I
use is
>     with the melt() function in the reshape2 package.
>     For example,
>
>     library(reshape2)
>     plotDF <- melt(SPYdf,
>                         id.vars="Date", # variables to replicate
>                         measure.vars=c("close", "volume"), #
>     variables to create index from
> 

Re: [R] substr gives empty output

2018-01-21 Thread Ek Esawi
The reason you get "" is, as stated on the previous response and on
the documentation of substr function, the function "When extracting,
if start is larger than the string length then "" is returned.". This
is what happens on your function.

HTH

EK

On Sun, Jan 21, 2018 at 3:59 AM, Luigi Marongiu
 wrote:
> Dear all,
> I have a string, let's say "testing", and I would like to extract in
> sequence each letter (character) from it. But when I use substr() I only
> properly get the first character, the rest is empty (""). What am I getting
> wrong?
> For example, I have this code:
>

> x <- "testing"
> k <- nchar(x)
> for (i in 1:k) {
>   y <- substr(x, i, 1)
>   print(y)
> }
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] substr gives empty output

2018-01-21 Thread Ted Harding
On Sun, 2018-01-21 at 09:59 +0100, Luigi Marongiu wrote:
> Dear all,
> I have a string, let's say "testing", and I would like to extract in
> sequence each letter (character) from it. But when I use substr() I only
> properly get the first character, the rest is empty (""). What am I getting
> wrong?
> For example, I have this code:
> 
> >>>
> x <- "testing"
> k <- nchar(x)
> for (i in 1:k) {
>   y <- substr(x, i, 1)
>   print(y)
> }

>From the help page
  substr(x, start, stop)
where 'start' is the position in the character vector x at which the
substring starts, and 'stop' is the position at which it stops.

Hence 'stop' must be >= 'start'; and if they are equal then you get
just the single character. That is the case in your code, when i=1;
when i > 1 then stop < start, so you get nothing. Compare with:

  x <- "testing"
  k <- nchar(x)
  for (i in 1:k) { 
y <- substr(x, i, i)  ### was: substr(x, i, 1)
print(y)
  }

[1] "t"
[1] "e"
[1] "s"
[1] "t"
[1] "i"
[1] "n"
[1] "g"

Hoping this helps,
Ted.

__
R-help@r-project.org 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.


Re: [R] [FORGED] substr gives empty output

2018-01-21 Thread Rolf Turner


On 21/01/18 21:59, Luigi Marongiu wrote:

Dear all,
I have a string, let's say "testing", and I would like to extract in
sequence each letter (character) from it. But when I use substr() I only
properly get the first character, the rest is empty (""). What am I getting
wrong?


What you're getting wrong is failing the read the help for substr(). 
The third argument is "stop", not the length of the substring.



For example, I have this code:




x <- "testing"
k <- nchar(x)
for (i in 1:k) {
   y <- substr(x, i, 1)
   print(y)
}


You want y <- substr(x,i,i).

cheers,

Rolf Turner

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

__
R-help@r-project.org 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.


[R] substr gives empty output

2018-01-21 Thread Luigi Marongiu
Dear all,
I have a string, let's say "testing", and I would like to extract in
sequence each letter (character) from it. But when I use substr() I only
properly get the first character, the rest is empty (""). What am I getting
wrong?
For example, I have this code:

>>>
x <- "testing"
k <- nchar(x)
for (i in 1:k) {
  y <- substr(x, i, 1)
  print(y)
}

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.