Re: [R] Customise Plot tick label on time series plot using date series

2023-01-13 Thread roslinazairimah zakaria
Hi Rui,

Thank you very much. It works beautifully.

On Fri, Jan 13, 2023 at 3:36 PM Rui Barradas  wrote:

> Às 05:11 de 13/01/2023, roslinazairimah zakaria escreveu:
> > Hi,
> >
> > I would like to customise my date series on the plot. I tried this:
> >
> > dt_ts <- ts(dt)
> > autoplot(dt_ts[,2]) + ylab("Charge counts") + xlab("Daily")
> >
> > but the label is not the date series.
> >
> > Tqvm for any help given.
> >
> >
> >> dput(dt)
> > structure(list(time = c("1/1/2014", "2/1/2014", "3/1/2014", "4/1/2014",
> > "5/1/2014", "6/1/2014", "7/1/2014", "8/1/2014", "9/1/2014", "10/1/2014",
> > "11/1/2014", "12/1/2014", "13/1/2014", "14/1/2014", "15/1/2014",
> > "16/1/2014", "17/1/2014", "18/1/2014", "19/1/2014", "20/1/2014",
> > "21/1/2014", "22/1/2014", "23/1/2014", "24/1/2014", "25/1/2014",
> > "26/1/2014", "27/1/2014", "28/1/2014", "29/1/2014", "30/1/2014",
> > "31/1/2014", "1/2/2014", "2/2/2014", "3/2/2014", "4/2/2014",
> > "5/2/2014", "6/2/2014", "7/2/2014", "8/2/2014", "9/2/2014", "10/2/2014",
> > "11/2/2014", "12/2/2014", "13/2/2014", "14/2/2014", "15/2/2014",
> > "16/2/2014", "17/2/2014", "18/2/2014", "19/2/2014", "20/2/2014",
> > "21/2/2014", "22/2/2014", "23/2/2014", "24/2/2014", "25/2/2014",
> > "26/2/2014", "27/2/2014", "28/2/2014", "1/3/2014", "2/3/2014",
> > "3/3/2014", "4/3/2014", "5/3/2014", "6/3/2014", "7/3/2014", "8/3/2014",
> > "9/3/2014", "10/3/2014", "11/3/2014", "12/3/2014", "13/3/2014",
> > "14/3/2014", "15/3/2014", "16/3/2014", "17/3/2014", "18/3/2014",
> > "19/3/2014", "20/3/2014", "21/3/2014", "22/3/2014", "23/3/2014",
> > "24/3/2014", "25/3/2014", "26/3/2014", "27/3/2014", "28/3/2014",
> > "29/3/2014", "30/3/2014", "31/3/2014", "1/4/2014", "2/4/2014",
> > "3/4/2014", "4/4/2014", "5/4/2014", "6/4/2014", "7/4/2014", "8/4/2014",
> > "9/4/2014", "10/4/2014", "11/4/2014", "12/4/2014", "13/4/2014",
> > "14/4/2014", "15/4/2014", "16/4/2014", "17/4/2014", "18/4/2014",
> > "19/4/2014", "20/4/2014", "21/4/2014", "22/4/2014", "23/4/2014",
> > "24/4/2014", "25/4/2014", "26/4/2014", "27/4/2014", "28/4/2014",
> > "29/4/2014", "30/4/2014", "1/5/2014", "2/5/2014", "3/5/2014",
> > "4/5/2014", "5/5/2014", "6/5/2014", "7/5/2014", "8/5/2014", "9/5/2014",
> > "10/5/2014", "11/5/2014", "12/5/2014", "13/5/2014", "14/5/2014",
> > "15/5/2014", "16/5/2014", "17/5/2014", "18/5/2014", "19/5/2014",
> > "20/5/2014", "21/5/2014", "22/5/2014", "23/5/2014", "24/5/2014",
> > "25/5/2014", "26/5/2014", "27/5/2014", "28/5/2014", "29/5/2014",
> > "30/5/2014", "31/5/2014", "1/6/2014", "2/6/2014", "3/6/2014",
> > "4/6/2014", "5/6/2014", "6/6/2014", "7/6/2014", "8/6/2014", "9/6/2014",
> > "10/6/2014", "11/6/2014", "12/6/2014", "13/6/2014", "14/6/2014",
> > "15/6/2014", "16/6/2014", "17/6/2014", "18/6/2014", "19/6/2014",
> > "20/6/2014", "21/6/2014", "22/6/2014", "23/6/2014", "24/6/2014",
> > "25/6/2014", "26/6/2014", "27/6/2014", "28/6/2014", "29/6/2014",
> > "30/6/2014", "1/7/2014", "2/7/2014", "3/7/2014", "4/7/2014",
> > "5/7/2014", "6/7/2014", "7/7/2014", "8/7/2014", "9/7/2014", "10/7/2014",
> > "11/7/2014", "12/7/2014", "13/7/2014", "14/7/2014", "15/7/2014",
> > "16/7/2014", "17/7/2014", "18/7/2014", "19/7/2014", "20/7/2014",
> > "21/7/2014", "22/7/2014", "23/7/2014", "24/7/2014", "25/7/2014",
> > "26/7/2014", "27/7/2014", "28/7/2014", "29/7/2014", "30/7/2014",
> > "31/7/2014", "1/8/2014", "2/8/2014", "3/8/2014", "4/8/2014",
> > "5/8/2014", "6/8/2014", "7/8/2014", "8/8/2014", "9/8/2014", "10/8/2014",
> > "11/8/2014", "12/8/2014", "13/8/2014", "14/8/2014", "15/8/2014",
> > "16/8/2014", "17/8/2014", "18/8/2014", "19/8/2014", "20/8/2014",
> > "21/8/2014", "22/8/2014", "23/8/2014", "24/8/2014", "25/8/2014",
> > "26/8/2014", "27/8/2014", "28/8/2014", "29/8/2014", "30/8/2014",
> > "31/8/2014", "1/9/2014", "2/9/2014", "3/9/2014", "4/9/2014",
> > "5/9/2014", "6/9/2014", "7/9/2014", "8/9/2014", "9/9/2014", "10/9/2014",
> > "11/9/2014", "12/9/2014", "13/9/2014", "14/9/2014", "15/9/2014",
> > "16/9/2014", "17/9/2014", "18/9/2014", "19/9/2014", "20/9/2014",
> > "21/9/2014", "22/9/2014", "23/9/2014", "24/9/2014", "25/9/2014",
> > "26/9/2014", "27/9/2014", "28/9/2014", "29/9/2014", "30/9/2014",
> > "1/10/2014", "2/10/2014", "3/10/2014", "4/10/2014", "5/10/2014",
> > "6/10/2014", "7/10/2014", "8/10/2014", "9/10/2014", "10/10/2014",
> > "11/10/2014", "12/10/2014", "13/10/2014", "14/10/2014", "15/10/2014",
> > "16/10/2014", "17/10/2014", "18/10/2014", "19/10/2014", "20/10/2014",
> > "21/10/2014", "22/10/2014", "23/10/2014", "24/10/2014", "25/10/2014",
> > "26/10/2014", "27/10/2014", "28/10/2014", "29/10/2014", "30/10/2014",
> > "31/10/2014", "1/11/2014", "2/11/2014", "3/11/2014", "4/11/2014",
> > "5/11/2014", "6/11/2014", "7/11/2014", "8/11/2014", "9/11/2014",
> > "10/11/2014", "11/11/2014", "12/11/2014", "13/11/2014", "14/11/2014",
> > "15/11/2014", "16/11/2014", "17/11/2014", "18/11/2014", "19/11/2014",
> > "20/11/2014", "21/11/2014", "22/11/2014", "23/11/2014", "24/11/2014",
> > "25/11/2014", "26/11/2014", "27/11/2014", "28/11/2014",

Re: [R] return value of {....}

2023-01-13 Thread Valentin Petzel
Hello Akshay,

R is quite inspired by LISP, where this is a common thing. It is not in fact 
that {...} returned something, rather any expression evalulates to some value, 
and for a compound statement that is the last evaluated expression.

{...} might be seen as similar to LISPs (begin ...).

Now this is a very different thing compared to {...} in something like C, even 
if it looks or behaves similarly. But in R {...} is in fact an expression and 
thus has evaluate to some value. This also comes with some nice benefits.

You do not need to use {...} for anything that is a single statement. But you 
can in each possible place use {...} to turn multiple statements into one.

Now think about a statement like this

f <- function(n) {
x <- runif(n)
x**2
}

Then we can do

y <- f(10)

Now, you suggested way would look like this:

f <- function(n) {
x <- runif(n)
y <- x**2
}

And we'd need to do something like:

f(10)
y <- somehow_get_last_env_of_f$y

So having a compound statement evaluate to a value clearly has a benefit.

Best Regards,
Valentin

09.01.2023 18:05:58 akshay kulkarni :

> Dear Valentin,
>                           But why should {} "return" a value? It could 
> just as well evaluate all the expressions and store the resulting objects in 
> whatever environment the interpreter chooses, and then it would be left to 
> the user to manipulate any object he chooses. Don't you think returning the 
> last, or any value, is redundant? We are living in the 21st century world, 
> and the R-core team might,I suppose, have a definite reason for"returning" 
> the last value. Any comments?
> 
> Thanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> 
> *From:* Valentin Petzel 
> *Sent:* Monday, January 9, 2023 9:18 PM
> *To:* akshay kulkarni 
> *Cc:* R help Mailing list 
> *Subject:* Re: [R] return value of {}
>  
> Hello Akshai,
> 
> I think you are confusing {...} with local({...}). This one will evaluate the 
> expression in a separate environment, returning the last expression.
> 
> {...} simply evaluates multiple expressions as one and returns the result of 
> the last line, but it still evaluates each expression.
> 
> Assignment returns the assigned value, so we can chain assignments like this
> 
> a <- 1 + (b <- 2)
> 
> conveniently.
> 
> So when is {...} useful? Well, anyplace where you want to execute complex 
> stuff in a function argument. E.g. you might do:
> 
> data %>% group_by(x) %>% summarise(y = {if(x[1] > 10) sum(y) else mean(y)})
> 
> Regards,
> Valentin Petzel
> 
> 09.01.2023 15:47:53 akshay kulkarni :
> 
>> Dear members,
>>  I have the following code:
>>
>>> TB <- {x <- 3;y <- 5}
>>> TB
>> [1] 5
>>
>> It is consistent with the documentation: For {, the result of the last 
>> expression evaluated. This has the visibility of the last evaluation.
>>
>> But both x AND y are created, but the "return value" is y. How can this be 
>> advantageous for solving practical problems? Specifically, consider the 
>> following code:
>>
>> F <- function(X) {  expr; expr2; { expr5; expr7}; expr8;expr10}
>>
>> Both expr5 and expr7 are created, and are accessible by the code outside of 
>> the nested braces right? But the "return value" of the nested braces is 
>> expr7. So doesn't this mean that only expr7 should be accessible? Please 
>> help me entangle this (of course the return value of F is expr10, and all 
>> the other objects created by the preceding expressions are deleted. But 
>> expr5 is not, after the control passes outside of the nested braces!)
>>
>> Thanking you,
>> Yours sincerely,
>> AKSHAY M KULKARNI
>>
>>     [[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.

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


Re: [R] return value of {....}

2023-01-13 Thread Heinz Tuechler

09.01.2023 18:05:58 akshay kulkarni :

We are living in the 21st century world, and the R-core team might,I suppose, 
have a definite reason ...



Maybe compatibility reasons with S and R-versions from the 20st century?
But maybe, you would have expected some reason even then.

best regards,

Heinz

__
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] return value of {....}

2023-01-13 Thread Bill Dunlap
R's
   { expr1; expr2; expr3}
acts much like C's
   ( expr1, expr2, expr3)

E.g.,

$ cat a.c
#include 

int main(int argc, char* argv[])
{
double y = 10 ;
double x = (printf("Starting... "), y = y + 100, y * 20);
printf("Done: x=%g, y=%g\n", x, y);
return 0;
}
$ gcc -Wall a.c
$ ./a.out
Starting... Done: x=2200, y=110

I don't like that syntax (e.g., commas between expressions instead of the
usual semicolons after expressions).  Perhaps John Chambers et all didn't
either.

-Bill

On Fri, Jan 13, 2023 at 2:28 AM Valentin Petzel  wrote:

> Hello Akshay,
>
> R is quite inspired by LISP, where this is a common thing. It is not in
> fact that {...} returned something, rather any expression evalulates to
> some value, and for a compound statement that is the last evaluated
> expression.
>
> {...} might be seen as similar to LISPs (begin ...).
>
> Now this is a very different thing compared to {...} in something like C,
> even if it looks or behaves similarly. But in R {...} is in fact an
> expression and thus has evaluate to some value. This also comes with some
> nice benefits.
>
> You do not need to use {...} for anything that is a single statement. But
> you can in each possible place use {...} to turn multiple statements into
> one.
>
> Now think about a statement like this
>
> f <- function(n) {
> x <- runif(n)
> x**2
> }
>
> Then we can do
>
> y <- f(10)
>
> Now, you suggested way would look like this:
>
> f <- function(n) {
> x <- runif(n)
> y <- x**2
> }
>
> And we'd need to do something like:
>
> f(10)
> y <- somehow_get_last_env_of_f$y
>
> So having a compound statement evaluate to a value clearly has a benefit.
>
> Best Regards,
> Valentin
>
> 09.01.2023 18:05:58 akshay kulkarni :
>
> > Dear Valentin,
> >   But why should {} "return" a value? It
> could just as well evaluate all the expressions and store the resulting
> objects in whatever environment the interpreter chooses, and then it would
> be left to the user to manipulate any object he chooses. Don't you think
> returning the last, or any value, is redundant? We are living in the
> 21st century world, and the R-core team might,I suppose, have a definite
> reason for"returning" the last value. Any comments?
> >
> > Thanking you,
> > Yours sincerely,
> > AKSHAY M KULKARNI
> >
> > 
> > *From:* Valentin Petzel 
> > *Sent:* Monday, January 9, 2023 9:18 PM
> > *To:* akshay kulkarni 
> > *Cc:* R help Mailing list 
> > *Subject:* Re: [R] return value of {}
> >
> > Hello Akshai,
> >
> > I think you are confusing {...} with local({...}). This one will
> evaluate the expression in a separate environment, returning the last
> expression.
> >
> > {...} simply evaluates multiple expressions as one and returns the
> result of the last line, but it still evaluates each expression.
> >
> > Assignment returns the assigned value, so we can chain assignments like
> this
> >
> > a <- 1 + (b <- 2)
> >
> > conveniently.
> >
> > So when is {...} useful? Well, anyplace where you want to execute
> complex stuff in a function argument. E.g. you might do:
> >
> > data %>% group_by(x) %>% summarise(y = {if(x[1] > 10) sum(y) else
> mean(y)})
> >
> > Regards,
> > Valentin Petzel
> >
> > 09.01.2023 15:47:53 akshay kulkarni :
> >
> >> Dear members,
> >>  I have the following code:
> >>
> >>> TB <- {x <- 3;y <- 5}
> >>> TB
> >> [1] 5
> >>
> >> It is consistent with the documentation: For {, the result of the last
> expression evaluated. This has the visibility of the last evaluation.
> >>
> >> But both x AND y are created, but the "return value" is y. How can this
> be advantageous for solving practical problems? Specifically, consider the
> following code:
> >>
> >> F <- function(X) {  expr; expr2; { expr5; expr7}; expr8;expr10}
> >>
> >> Both expr5 and expr7 are created, and are accessible by the code
> outside of the nested braces right? But the "return value" of the nested
> braces is expr7. So doesn't this mean that only expr7 should be accessible?
> Please help me entangle this (of course the return value of F is expr10,
> and all the other objects created by the preceding expressions are deleted.
> But expr5 is not, after the control passes outside of the nested braces!)
> >>
> >> Thanking you,
> >> Yours sincerely,
> >> AKSHAY M KULKARNI
> >>
> >> [[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.
>
> [[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 gu

[R] Removing variables from data frame with a wile card

2023-01-13 Thread Steven T. Yen

I have a data frame containing variables "yr3",...,"yr28".

How do I remove them with a wild cardsomething similar to "del yr*" 
in Windows/doc? Thank you.


> colnames(mydata)
  [1] "year"   "weight" "confeduc"   "confothr" "college"
  [6] ...
 [41] "yr3"    "yr4"    "yr5"    "yr6" "yr7"
 [46] "yr8"    "yr9"    "yr10"   "yr11" "yr12"
 [51] "yr13"   "yr14"   "yr15"   "yr16" "yr17"
 [56] "yr18"   "yr19"   "yr20"   "yr21" "yr22"
 [61] "yr23"   "yr24"   "yr25"   "yr26" "yr27"
 [66] "yr28"...

__
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] Removing variables from data frame with a wile card

2023-01-13 Thread Eric Berger
mydata[, -grep("^yr",colnames(mydata))]

On Sat, Jan 14, 2023 at 8:57 AM Steven T. Yen  wrote:

> I have a data frame containing variables "yr3",...,"yr28".
>
> How do I remove them with a wild cardsomething similar to "del yr*"
> in Windows/doc? Thank you.
>
>  > colnames(mydata)
>[1] "year"   "weight" "confeduc"   "confothr" "college"
>[6] ...
>   [41] "yr3""yr4""yr5""yr6" "yr7"
>   [46] "yr8""yr9""yr10"   "yr11" "yr12"
>   [51] "yr13"   "yr14"   "yr15"   "yr16" "yr17"
>   [56] "yr18"   "yr19"   "yr20"   "yr21" "yr22"
>   [61] "yr23"   "yr24"   "yr25"   "yr26" "yr27"
>   [66] "yr28"...
>
> __
> 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.
>

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


Re: [R] Removing variables from data frame with a wile card

2023-01-13 Thread Andrew Simmons
You'll want to use grep() or grepl(). By default, grep() uses extended
regular expressions to find matches, but you can also use perl regular
expressions and globbing (after converting to a regular expression).
For example:

grepl("^yr", colnames(mydata))

will tell you which 'colnames' start with "yr". If you'd rather you
use globbing:

grepl(glob2rx("yr*"), colnames(mydata))

Then you might write something like this to remove the columns starting with yr:

mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE]

On Sat, Jan 14, 2023 at 1:56 AM Steven T. Yen  wrote:
>
> I have a data frame containing variables "yr3",...,"yr28".
>
> How do I remove them with a wild cardsomething similar to "del yr*"
> in Windows/doc? Thank you.
>
>  > colnames(mydata)
>[1] "year"   "weight" "confeduc"   "confothr" "college"
>[6] ...
>   [41] "yr3""yr4""yr5""yr6" "yr7"
>   [46] "yr8""yr9""yr10"   "yr11" "yr12"
>   [51] "yr13"   "yr14"   "yr15"   "yr16" "yr17"
>   [56] "yr18"   "yr19"   "yr20"   "yr21" "yr22"
>   [61] "yr23"   "yr24"   "yr25"   "yr26" "yr27"
>   [66] "yr28"...
>
> __
> 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.