[R] Modificatio of function body within a function

2022-04-01 Thread Grzegorz Smoliński
Hi,

I'm working on a presentation regarding the modification of the
function body on-the-fly and just discovered something I don't
understand. I would like to modify a body of the function within a
function, but I see that it is possible only when explicitly referring
to the environment where the function is defined:

fun1 <- function() {
  1
  body(fun1)[[2]] <- "one"
}
fun1()
body(fun1)
#> {
#> 1
#> body(fun1)[[2]] <- "one"
#> }

fun2 <- function() {
  2
  env <- environment(fun2)
  body(env$fun2)[[2]] <- "two"
}
fun2()
body(fun2)
#> {
#> "two"
#> env <- environment(fun2)
#> body(env$fun2)[[2]] <- "two"
#> }

Can I get some explanation or some links / articles about this,
please? I thought it won't be a difference and I should be able to
modify a function body also in the first case, because I'm changing
something in the parent environment being in the child environment.

Best regards,

Grzegorz

__
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] Recommended Statistics Papers and Packages

2022-04-01 Thread Stephen H. Dawson, DSL via R-help

Good Morning,


I have been doing some further reading on the topic of statistics 
lately. I enjoyed reading the following material. I am writing to share 
the material with the list as recommended reading on the respective topics.



Estimating the number of clusters in a data set via the gap statistic
https://hastie.su.domains/Papers/gap.pdf



Package ‘FNN’
https://cran.r-project.org/web/packages/FNN/FNN.pdf



The Relationship Between Precision-Recall and ROC Curves
https://www.biostat.wisc.edu/~page/rocpr.pdf


Enjoy!


Kindest Regards
--
*Stephen Dawson, DSL*
/Executive Strategy Consultant/
Business & Technology
+1 (865) 804-3454
http://www.shdawson.com

__
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] Violin plots in R

2022-04-01 Thread PIKAL Petr
Hi.

Beside advice of others, at least 

str(yourdata)
and the ggplot code is minimum for us to be able to offer some advice.

Preferable way how to send your data is output from

dput(yourdata) or dput(head(yourdata))

Just copy paste to your email.

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Ebert,Timothy
> Aaron
> Sent: Friday, April 1, 2022 11:35 AM
> To: pooja sinha ; r-help mailing list  project.org>
> Subject: Re: [R] Violin plots in R
> 
> No data. Attachments are removed. Send only text. Include program. Did you
> look at geom_violin() ? try http://www.sthda.com/english/wiki/ggplot2-
> violin-plot-quick-start-guide-r-software-and-data-visualization
> 
> If geom_point() is working then geom_violin() should also work. If neither
> work then the problem is earlier in the program. Possibly in the ggplot()
> statement, or earlier.
> Tim
> 
> -Original Message-
> From: R-help  On Behalf Of pooja sinha
> Sent: Thursday, March 31, 2022 7:00 PM
> To: r-help mailing list 
> Subject: [R] Violin plots in R
> 
> [External Email]
> 
> Hi All,
> 
> I need your help in making the violin plot in R using the data which is
> attached herewith. I am new to R and having issues in tidying my data for
R. I
> am trying the code but I am not able to tidy my data for violin plot in
ggplot.
> 
> Any help will be highly appreciated.
> 
> Thanks,
> Puja
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-
> 3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-
> zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=ewsSBA7yD-
> EGNgEo5uwx8ypGde2s8cN0NqRJcjUeat_oxJOzxc4u1RoS4APbTp-G=5RT-
> EbmPWAiqxd-tCyTCK3tUPOmNomYvRoE-pgEKXMk=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-
> 2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-
> zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=ewsSBA7yD-
> EGNgEo5uwx8ypGde2s8cN0NqRJcjUeat_oxJOzxc4u1RoS4APbTp-
> G=FhxhDOcldxc5mIg1wiE5kq4D_sxIm1Ho0PbejAHl8xY=
> 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-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] Violin plots in R

2022-04-01 Thread pooja sinha
Hi All,

I need your help in making the violin plot in R using the data which is
attached herewith. I am new to R and having issues in tidying my data for
R. I am trying the code but I am not able to tidy my data for violin plot
in ggplot.

Any help will be highly appreciated.

Thanks,
Puja
__
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] readxl, read_excel: how colon (:) is read ?

2022-04-01 Thread Jeff Newmiller
Why not simply let readxl read the timestamp as it will naturally and use

dta$Arrival_time_c <- as.character( dta$Arrival_time, format = "%H:%M" )

... though time data in such a form is remarkably useless for data analysis.

On April 1, 2022 12:09:02 AM PDT, Rui Barradas  wrote:
>Hello,
>
>The following function is probably not very solid, to work with dates 
>and times is not trivial, but it converts those Excel numbers correctly.
>
>I don't know with what numbers it fails.
>
>
>xl_fracday_to_time <- function(x, digits = 0L) {
>   old_opts <- options(digits = 20)
>   on.exit(options(old_opts))
>   if(is.character(x)) {
> x <- as.numeric(x)
>   }
>   y <- x * 24
>   hours <- floor(y + .Machine$double.eps^0.5)
>   y <- (y - hours) * 60
>   mins <- floor(y + .Machine$double.eps^0.5)
>   y <- (y - mins) * 60
>   secs <- round(y, digits = digits)
>   sprintf("%02d:%02d:%02d", hours, mins, secs)
>}
>
>x <- c("0.568750009", "0.577083328")
>xl_fracday_to_time(x)
>#[1] "13:39:00" "13:51:00"
>
>
>Hope this helps,
>
>Rui Barradas
>
>Às 07:12 de 01/04/2022, Patrick Giraudoux escreveu:
>> I have a unexpected behaviour reading times with colon from an Excel
>> file, using the package readxl.
>> 
>> In an Excel sheet, I have a column with times in hours:minutes, e.g:
>> 
>> Arrival_time
>> 13:39
>> 13:51
>> 
>> When read from R with readxl::read_excel, this gives a tibble column
>> with full date by defaut being the last day of 1899. OK. Why not, I know
>> that POSIX variables are starting in 1900 after R doc (however I wonder
>> why here the defaut is one day before January 1, 1900
>> 
>>> tmp$Arrival_time  [1] "1899-12-31 13:39:00 UTC" "1899-12-31 13:51:00 UTC"
>> 
>> Well, this is not exactly what I want to. I do not care about the year
>> and the day... Therefore I decided to import this column as "text"
>> explicitely (in order to manage it within R then). And this is what I
>> get now:
>> 
>>> read_excel("saisie_data_durban_rapaces_LPO.xlsx",sheet=2,col_types="text")  
>>> > tmp$Arrival_time [1] "0.568750009" "0.577083328"
>> 
>> Can someone tell me what happens ?
>> 
>> I would really appreciate to understand the trick...
>> 
>> 
>>  [[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.

-- 
Sent from my phone. Please excuse my brevity.

__
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] readxl, read_excel: how colon (:) is read ?

2022-04-01 Thread Rui Barradas

Hello,

The following function is probably not very solid, to work with dates 
and times is not trivial, but it converts those Excel numbers correctly.


I don't know with what numbers it fails.


xl_fracday_to_time <- function(x, digits = 0L) {
  old_opts <- options(digits = 20)
  on.exit(options(old_opts))
  if(is.character(x)) {
x <- as.numeric(x)
  }
  y <- x * 24
  hours <- floor(y + .Machine$double.eps^0.5)
  y <- (y - hours) * 60
  mins <- floor(y + .Machine$double.eps^0.5)
  y <- (y - mins) * 60
  secs <- round(y, digits = digits)
  sprintf("%02d:%02d:%02d", hours, mins, secs)
}

x <- c("0.568750009", "0.577083328")
xl_fracday_to_time(x)
#[1] "13:39:00" "13:51:00"


Hope this helps,

Rui Barradas

Às 07:12 de 01/04/2022, Patrick Giraudoux escreveu:

I have a unexpected behaviour reading times with colon from an Excel
file, using the package readxl.

In an Excel sheet, I have a column with times in hours:minutes, e.g:

Arrival_time
13:39
13:51

When read from R with readxl::read_excel, this gives a tibble column
with full date by defaut being the last day of 1899. OK. Why not, I know
that POSIX variables are starting in 1900 after R doc (however I wonder
why here the defaut is one day before January 1, 1900


tmp$Arrival_time  [1] "1899-12-31 13:39:00 UTC" "1899-12-31 13:51:00 UTC"


Well, this is not exactly what I want to. I do not care about the year
and the day... Therefore I decided to import this column as "text"
explicitely (in order to manage it within R then). And this is what I
get now:


read_excel("saisie_data_durban_rapaces_LPO.xlsx",sheet=2,col_types="text")  > tmp$Arrival_time [1] 
"0.568750009" "0.577083328"


Can someone tell me what happens ?

I would really appreciate to understand the trick...


[[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] readxl, read_excel: how colon (:) is read ?

2022-04-01 Thread Patrick Giraudoux
This can be made using the TEXT (TEXTE in the French version) function 
of Excel, hence:

TEXT(M2;"HH:MM")

Changes the time into text, and it can be imported from R as wanted.



Le 01/04/2022 à 08:34, Patrick Giraudoux a écrit :
> Absolutely correct ! I checked in Excel and when I change the format 
> to "text", then I get in Excel the same fractional numbers as those 
> obtained importing text from R... Hence the issue comes from Excel 
> itself. Will find a way to change this format to text in Excel without 
> avoiding such conversion...
> Thanks Andrew !
>
> Le 01/04/2022 à 08:26, Andrew Simmons a écrit :
>> Probably (but not entirely sure), Excel is storing your text as a 
>> number of days, so 13:38 is a little more than half a day. Open your 
>> spreadsheet in excel and save those columns as text instead of times, 
>> that (should) fix your issue.
>>
>> On Fri, Apr 1, 2022, 02:12 Patrick Giraudoux 
>>  wrote:
>>
>> I have a unexpected behaviour reading times with colon from an Excel
>> file, using the package readxl.
>>
>> In an Excel sheet, I have a column with times in hours:minutes, e.g:
>>
>> Arrival_time
>> 13:39
>> 13:51
>>
>> When read from R with readxl::read_excel, this gives a tibble column
>> with full date by defaut being the last day of 1899. OK. Why not,
>> I know
>> that POSIX variables are starting in 1900 after R doc (however I
>> wonder
>> why here the defaut is one day before January 1, 1900
>>
>> > tmp$Arrival_time  [1] "1899-12-31 13:39:00 UTC" "1899-12-31
>> 13:51:00 UTC"
>>
>> Well, this is not exactly what I want to. I do not care about the
>> year
>> and the day... Therefore I decided to import this column as "text"
>> explicitely (in order to manage it within R then). And this is
>> what I
>> get now:
>>
>> >
>> 
>> read_excel("saisie_data_durban_rapaces_LPO.xlsx",sheet=2,col_types="text")
>> > tmp$Arrival_time [1] "0.568750009" "0.577083328"
>>
>> Can someone tell me what happens ?
>>
>> I would really appreciate to understand the trick...
>>
>>
>>         [[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] readxl, read_excel: how colon (:) is read ?

2022-04-01 Thread Patrick Giraudoux
Le 01/04/2022 à 08:40, Jeff Newmiller a écrit :
> Both R and Excel assume a date is associated with every time object. In 
> Excel, when you show a date it is an integer number of days since 1899-12-31 
> (due to a mistake made early in programming it). Whenever you show a time, it 
> it merely displaying the time portion (fraction of a day) of a date/time. The 
> date part of that value may or may not be 1899-12-31.
>
> With this in mind, you are tilting at windmills hoping to import a "pure 
> time" because no such thing exists in either program. You can choose to 
> render a `POSIXct` as showing only the time portion when you convert it to 
> character if you so choose.

Thanks for the infos. Yes, this is exactly what  I did yesterday with 
POSIXct > POSIXlt to go ahead. However I wanted to understand fully what 
happened, hence the call to the list. Jeff and Andrew, now eveything is 
clear to me thanks to you...


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