Re: [R] save(), load(), saveRDS(), and readRDS()

2023-09-29 Thread Jorgen Harmse via R-help
Ivan Krylov points out that load(file, e <- new.env()) is cumbersome. I put it 
into a function.

Regards,
Jorgen Harmse.


#' Save & load lists & environments

#'

#' \code{\link{save}} has to be told what to save from an environment, and the 
obvious way

#' to save a structure creates an extra layer. \code{\link{load}} with default 
settings

#' clobbers the current environment. \code{save.env} saves a list or 
environment without an

#' extra layer, and by default saves everything. \code{load.env} loads into an 
environment,

#' and \code{load.list} loads into a list.

#'

#' @param S something that can be coerced to an environment, e.g. a named 
\code{list}

#' @param file,envir inputs to \code{save} or \code{load}

#' @param list input to \code{save}

#' @param skip variables in \code{envir} that should not be saved, ignored if 
\code{list}

#' is provided

#' @param ... inputs to \code{load.env} or additional inputs to \code{save}

#'

#' @return \code{invisible} from \code{save.env}; an \code{environment} from 
\code{load.env};

#' a \code{list} from \code{load.list}

#'

#' @export



save.env <- function( S, file, list = setdiff(ls(envir),skip),

  envir = if(missing(S)) parent.frame() else 
as.environment(S),

  skip=NULL, ...

)

{ save(list=list, file=file, envir=envir, ...)}



#' @rdname save.env

#'

#' @param keep,remove names of variables to keep or to remove

#' @param absent what to do if variables named in \code{keep} are absent

#' @param parent input to \code{\link{new.env}}

#'

#' @note \code{remove} is forced after the file is loaded, so the default works 
correctly.

#'

#' @export



load.env <- function( file, keep, remove = if(!missing(keep)) 
setdiff(ls(envir),keep),

  absent=c('warn','ignore','stop'), 
envir=new.env(parent=parent),

  parent=parent.frame()

)

{ load(file,envir)

  rm(list=remove,envir=envir)

  if ( !missing(keep) && (match.arg(absent) -> absent) != 'ignore'

  && length(keep.absent <- setdiff(keep,ls(envir))) > 0L )

  { print(keep.absent)

if (absent=='warning')

  warning('The variables listed above are absent from the file.')

else

  stop('The variables listed above are absent from the file.')

  }

  return(envir)

}



#' @rdname save.env

#'

#' @param all.names input to \code{\link{as.list}}

#'

#' @export



load.list <- function(..., all.names=TRUE) as.list(all.names=all.names, 
load.env(...))




--

Message: 2
Date: Fri, 29 Sep 2023 11:42:37 +0300
From: Ivan Krylov 
To: Shu Fai Cheung 
Cc: R mailing list 
Subject: Re: [R] save(), load(), saveRDS(), and readRDS()
Message-ID: <20230929114237.2592975a@Tarkus>
Content-Type: text/plain; charset="utf-8"

On Thu, 28 Sep 2023 23:46:45 +0800
Shu Fai Cheung  wrote:

> In my personal work, I prefer using saveRDS() and loadRDS() as I
> don't like the risk of overwriting anything in the global
> environment.

There's the load(file, e <- new.env()) idiom, but that's potentially
a lot to type.

*

[[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] lattice densityplot with weights

2023-09-29 Thread Deepayan Sarkar
Yes, the following should (mostly) work:

lattice::densityplot(~ x, mydf, groups = name, weights = wt,
auto.key = list(space = "inside"), grid = TRUE)

I just realised that the 'subdensity' and 'warnWbw' arguments are not
passed through by densityplot(), so there is no way to get rid of the
corresponding warnings. But this should be OK otherwise.

On Fri, 29 Sept 2023 at 10:35, Bert Gunter  wrote:
>
> Unless I misunderstand...
>
> See ?panel.densityplot
>
> Lattice functions do their work through a host of panel functions,
> typically passing their ... arguments to the panel functions.
> panel.densityplot has a weights argument.

'weights' needs a bit more work because it has to do NSE like
'groups', but other than that, yes. The ?densityplot help page
documents the 'weight' argument.

Best,
-Deepayan


> Cheers,
> Bert
>
>
>
>
> On Fri, Sep 29, 2023 at 3:32 AM Naresh Gurbuxani <
> naresh_gurbux...@hotmail.com> wrote:
>
> >
> > density() function in R accepts weights as an input.  Using this
> > function, one can calculate density and plot it.  Is it possible to
> > combined these two operations in lattice densityplot()?
> >
> > mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt =
> > diff(pnorm(seq(-3, 3, by = 0.2
> > mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt =
> > mydf$wt))
> > with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |>
> > plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots"))
> > with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |>
> > lines(lty = 2, col = 2))
> > grid()
> > legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2),
> > bty = "n")
> >
> > # I want to do something like this:
> > # densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l",
> > "g"))
> >
> > __
> > 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.

__
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] lattice densityplot with weights

2023-09-29 Thread Bert Gunter
Unless I misunderstand...

See ?panel.densityplot

Lattice functions do their work through a host of panel functions,
typically passing their ... arguments to the panel functions.
panel.densityplot has a weights argument.

Cheers,
Bert




On Fri, Sep 29, 2023 at 3:32 AM Naresh Gurbuxani <
naresh_gurbux...@hotmail.com> wrote:

>
> density() function in R accepts weights as an input.  Using this
> function, one can calculate density and plot it.  Is it possible to
> combined these two operations in lattice densityplot()?
>
> mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt =
> diff(pnorm(seq(-3, 3, by = 0.2
> mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt =
> mydf$wt))
> with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |>
> plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots"))
> with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |>
> lines(lty = 2, col = 2))
> grid()
> legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2),
> bty = "n")
>
> # I want to do something like this:
> # densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l",
> "g"))
>
> __
> 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] replace character by numeric value

2023-09-29 Thread Ebert,Timothy Aaron
You might also try
mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1,-1))

This may give unexpected results if there are other choices besides "BUY" and 
"SELL".

mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1, ifelse(side == 
'SELL', -1, NA)))

is an option that would take care of other options.



-Original Message-
From: R-help  On Behalf Of Ben Bolker
Sent: Friday, September 29, 2023 10:00 AM
To: r-help@r-project.org
Subject: Re: [R] replace character by numeric value

[External Email]

   The reason you're getting the result as character is that you have 'side' as 
your alternative result in the second ifelse().  If "BUY" and "SELL" are the 
only options you might try

ifelse(side == 'BUY', 1, ifelse(side == 'SELL', -1, NA))

or

c(1,-1)[match(side, c("BUY", "SELL"))]

or

vals <- c(BUY=1, SELL = -1)
vals[side]


On 2023-09-29 9:21 a.m., Ebert,Timothy Aaron wrote:
> Does this work?
> mynewdf$side <- as.numeric(mynewdf$side)
>
> This code would be the next line after your mutate.
>
> TIm
>
> -Original Message-
> From: R-help  On Behalf Of Enrico
> Schumann
> Sent: Thursday, September 28, 2023 3:13 AM
> To: arnaud gaboury 
> Cc: r-help 
> Subject: Re: [R] replace character by numeric value
>
> [External Email]
>
> On Wed, 27 Sep 2023, arnaud gaboury writes:
>
>> I have two data.frames:
>>
>> mydf1 <- structure(list(symbol = "ETHUSDT", cummulative_quote_qty =
>> 1999.9122, side = "BUY", time = structure(1695656875.805, tzone = "",
>> class = c("POSIXct", "POSIXt"))), row.names = c(NA, -1L), class =
>> c("data.table",
>> "data.frame"))
>>
>> mydf2 <- structure(list(symbol = c("ETHUSDT", "ETHUSDT", "ETHUSDT"),
>> cummulative_quote_qty = c(1999.119408, 0, 2999.890985), side =
>> c("SELL", "BUY", "BUY"), time = structure(c(1695712848.487,
>> 1695744226.993, 1695744509.082), class = c("POSIXct", "POSIXt"
>> ), tzone = "")), row.names = c(NA, -3L), class = c("data.table",
>> "data.frame"))
>>
>> I use this line to replace 'BUY' by numeric 1 and 'SELL' by numeric
>> -1 in
>> mydf1 and mydf2:
>> mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
>> ifelse(side == 'SELL', -1, side)))
>>
>> This does the job but I am left with an issue: 1 and -1 are
>> characters for
>> mynewdf2 when it is numeric for mynewdf1. The result I am expecting
>> is getting numeric values.
>> I can't solve this issue (using as.numeric(1) doesn't work) and don't
>> understand why I am left with num for mynewdf1 and characters for mynewdf2.
>>
>>> mynewdf1 <- mydf1 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
>> ifelse(side == 'SELL', -1, side)))
>>> str(mynewdf1)
>> Classes 'data.table' and 'data.frame': 1 obs. of  4 variables:
>>   $ symbol   : chr "ETHUSDT"
>>   $ cummulative_quote_qty: num 2000
>>   $ side : num 1  <<<--
>>   $ time : POSIXct, format: "2023-09-25 17:47:55"
>>   - attr(*, ".internal.selfref")=
>>
>>> mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
>> ifelse(side == 'SELL', -1, side)))
>>>   str(mynewdf2)
>> Classes 'data.table' and 'data.frame': 3 obs. of  4 variables:
>>   $ symbol   : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
>>   $ cummulative_quote_qty: num  1999 0 3000
>>   $ side : chr  "-1" "1" "1"   <<<--
>>   $ time : POSIXct, format: "2023-09-26 09:20:48"
>> "2023-09-26 18:03:46" "2023-09-26 18:08:29"
>>   - attr(*, ".internal.selfref")=
>>
>> Thank you for help
>>
>
> I'd use something like this:
>
>  map <- c(BUY = 1, SELL = -1)
>  mydf1$side <- map[mydf1$side]
>  str(mydf1)
>  ## Classes 'data.table' and 'data.frame':   1 obs. of  4 variables:
>  ##  $ symbol   : chr "ETHUSDT"
>  ##  $ cummulative_quote_qty: num 2000
>  ##  $ side : num 1
>
>  mydf2$side <- map[mydf2$side]
>  str(mydf2)
>  ## Classes 'data.table' and 'data.frame':   3 obs. of  4 variables:
>  ##  $ symbol   : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
>  ##  $ cummulative_quote_qty: num  1999 0 3000
>  ##  $ side : num  -1 1 1
>  ##  $ time : POSIXct, format: "2023-09-26 09:20:48" ...
>
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enric/
> oschumann.net%2F=05%7C01%7Ctebert%40ufl.edu%7Cc444bc296379414ba95
> b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63831592818
> 5843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLC
> JBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=YHs9vJMita%2BWAdlmn
> zx7%2Bte8gGqf3bTDl6CjnVG0exw%3D=0
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Ctebert%40ufl.edu
> %7Cc444bc296379414ba95b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638315928185843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> 

Re: [R] replace character by numeric value

2023-09-29 Thread Ben Bolker
  The reason you're getting the result as character is that you have 
'side' as your alternative result in the second ifelse().  If "BUY" and 
"SELL" are the only options you might try


   ifelse(side == 'BUY', 1, ifelse(side == 'SELL', -1, NA))

or

   c(1,-1)[match(side, c("BUY", "SELL"))]

or

vals <- c(BUY=1, SELL = -1)
vals[side]


On 2023-09-29 9:21 a.m., Ebert,Timothy Aaron wrote:

Does this work?
mynewdf$side <- as.numeric(mynewdf$side)

This code would be the next line after your mutate.

TIm

-Original Message-
From: R-help  On Behalf Of Enrico Schumann
Sent: Thursday, September 28, 2023 3:13 AM
To: arnaud gaboury 
Cc: r-help 
Subject: Re: [R] replace character by numeric value

[External Email]

On Wed, 27 Sep 2023, arnaud gaboury writes:


I have two data.frames:

mydf1 <- structure(list(symbol = "ETHUSDT", cummulative_quote_qty =
1999.9122, side = "BUY", time = structure(1695656875.805, tzone = "",
class = c("POSIXct", "POSIXt"))), row.names = c(NA, -1L), class =
c("data.table",
"data.frame"))

mydf2 <- structure(list(symbol = c("ETHUSDT", "ETHUSDT", "ETHUSDT"),
cummulative_quote_qty = c(1999.119408, 0, 2999.890985), side =
c("SELL", "BUY", "BUY"), time = structure(c(1695712848.487,
1695744226.993, 1695744509.082), class = c("POSIXct", "POSIXt"
), tzone = "")), row.names = c(NA, -3L), class = c("data.table",
"data.frame"))

I use this line to replace 'BUY' by numeric 1 and 'SELL' by numeric -1
in
mydf1 and mydf2:
mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
ifelse(side == 'SELL', -1, side)))

This does the job but I am left with an issue: 1 and -1 are characters
for
mynewdf2 when it is numeric for mynewdf1. The result I am expecting is
getting numeric values.
I can't solve this issue (using as.numeric(1) doesn't work) and don't
understand why I am left with num for mynewdf1 and characters for mynewdf2.


mynewdf1 <- mydf1 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,

ifelse(side == 'SELL', -1, side)))

str(mynewdf1)

Classes 'data.table' and 'data.frame': 1 obs. of  4 variables:
  $ symbol   : chr "ETHUSDT"
  $ cummulative_quote_qty: num 2000
  $ side : num 1  <<<--
  $ time : POSIXct, format: "2023-09-25 17:47:55"
  - attr(*, ".internal.selfref")=


mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,

ifelse(side == 'SELL', -1, side)))

  str(mynewdf2)

Classes 'data.table' and 'data.frame': 3 obs. of  4 variables:
  $ symbol   : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
  $ cummulative_quote_qty: num  1999 0 3000
  $ side : chr  "-1" "1" "1"   <<<--
  $ time : POSIXct, format: "2023-09-26 09:20:48"
"2023-09-26 18:03:46" "2023-09-26 18:08:29"
  - attr(*, ".internal.selfref")=

Thank you for help



I'd use something like this:

 map <- c(BUY = 1, SELL = -1)
 mydf1$side <- map[mydf1$side]
 str(mydf1)
 ## Classes 'data.table' and 'data.frame':   1 obs. of  4 variables:
 ##  $ symbol   : chr "ETHUSDT"
 ##  $ cummulative_quote_qty: num 2000
 ##  $ side : num 1

 mydf2$side <- map[mydf2$side]
 str(mydf2)
 ## Classes 'data.table' and 'data.frame':   3 obs. of  4 variables:
 ##  $ symbol   : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
 ##  $ cummulative_quote_qty: num  1999 0 3000
 ##  $ side : num  -1 1 1
 ##  $ time : POSIXct, format: "2023-09-26 09:20:48" ...



--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net/

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


--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
(Acting) Graduate chair, Mathematics & Statistics
> E-mail is sent at my convenience; I don't expect replies outside of 
working hours.


__
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] replace character by numeric value

2023-09-29 Thread Ebert,Timothy Aaron
Does this work?
mynewdf$side <- as.numeric(mynewdf$side)

This code would be the next line after your mutate.

TIm

-Original Message-
From: R-help  On Behalf Of Enrico Schumann
Sent: Thursday, September 28, 2023 3:13 AM
To: arnaud gaboury 
Cc: r-help 
Subject: Re: [R] replace character by numeric value

[External Email]

On Wed, 27 Sep 2023, arnaud gaboury writes:

> I have two data.frames:
>
> mydf1 <- structure(list(symbol = "ETHUSDT", cummulative_quote_qty =
> 1999.9122, side = "BUY", time = structure(1695656875.805, tzone = "",
> class = c("POSIXct", "POSIXt"))), row.names = c(NA, -1L), class =
> c("data.table",
> "data.frame"))
>
> mydf2 <- structure(list(symbol = c("ETHUSDT", "ETHUSDT", "ETHUSDT"),
> cummulative_quote_qty = c(1999.119408, 0, 2999.890985), side =
> c("SELL", "BUY", "BUY"), time = structure(c(1695712848.487,
> 1695744226.993, 1695744509.082), class = c("POSIXct", "POSIXt"
> ), tzone = "")), row.names = c(NA, -3L), class = c("data.table",
> "data.frame"))
>
> I use this line to replace 'BUY' by numeric 1 and 'SELL' by numeric -1
> in
> mydf1 and mydf2:
> mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
> ifelse(side == 'SELL', -1, side)))
>
> This does the job but I am left with an issue: 1 and -1 are characters
> for
> mynewdf2 when it is numeric for mynewdf1. The result I am expecting is
> getting numeric values.
> I can't solve this issue (using as.numeric(1) doesn't work) and don't
> understand why I am left with num for mynewdf1 and characters for mynewdf2.
>
>> mynewdf1 <- mydf1 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
> ifelse(side == 'SELL', -1, side)))
>> str(mynewdf1)
> Classes 'data.table' and 'data.frame': 1 obs. of  4 variables:
>  $ symbol   : chr "ETHUSDT"
>  $ cummulative_quote_qty: num 2000
>  $ side : num 1  <<<--
>  $ time : POSIXct, format: "2023-09-25 17:47:55"
>  - attr(*, ".internal.selfref")=
>
>> mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
> ifelse(side == 'SELL', -1, side)))
>>  str(mynewdf2)
> Classes 'data.table' and 'data.frame': 3 obs. of  4 variables:
>  $ symbol   : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
>  $ cummulative_quote_qty: num  1999 0 3000
>  $ side : chr  "-1" "1" "1"   <<<--
>  $ time : POSIXct, format: "2023-09-26 09:20:48"
> "2023-09-26 18:03:46" "2023-09-26 18:08:29"
>  - attr(*, ".internal.selfref")=
>
> Thank you for help
>

I'd use something like this:

map <- c(BUY = 1, SELL = -1)
mydf1$side <- map[mydf1$side]
str(mydf1)
## Classes 'data.table' and 'data.frame':   1 obs. of  4 variables:
##  $ symbol   : chr "ETHUSDT"
##  $ cummulative_quote_qty: num 2000
##  $ side : num 1

mydf2$side <- map[mydf2$side]
str(mydf2)
## Classes 'data.table' and 'data.frame':   3 obs. of  4 variables:
##  $ symbol   : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
##  $ cummulative_quote_qty: num  1999 0 3000
##  $ side : num  -1 1 1
##  $ time : POSIXct, format: "2023-09-26 09:20:48" ...



--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net/

__
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] Odd result

2023-09-29 Thread peter dalgaard
Yes. Also notice that something fishy seems to be going on in columns 2 and 3 
(assuming that the date/time is 1 column). 
They appear to be read as character data, even though the content is numeric?

-pd

> On 24 Sep 2023, at 11:58 , Michael Dewey  wrote:
> 
> Dear David
> 
> To get the first 46 rows just do KurtzData[1:43,]
> 
> However really you want to find out why it happened. It looks as though the 
> .csv file you read has lots of blank lines at the end. I would open it in an 
> editor to check that.
> 
> Michael
> 
> On 23/09/2023 23:55, Parkhurst, David wrote:
>> With help from several people, I used file.choose() to get my file name, and 
>> read.csv() to read in the file as KurtzData.  Then when I print KurtzData, 
>> the last several lines look like this:
>> 39   5/31/22  16.0  3411.75525 0.0201 0.0214   7.00
>> 40   6/28/22  2:00 PM  0.0  2150.67950 0.0156 0.0294 NA
>> 41   7/25/22 11:00 AM  11.9   1943.5NA NA 0.0500   7.80
>> 42   8/31/22  0220.5NA NA 0.0700  30.50
>> 43   9/28/22  0.067 10.9NA NA 0.0700  10.20
>> 44  10/26/22  0.086  237NA NA 0.1550  45.00
>> 45   1/12/23  1:00 PM 36.2624196NA NA 0.7500 283.50
>> 46   2/14/23  1:00 PM 20.71   55NA NA 0.0500   2.40
>> 47  NA NA NA NA
>> 48  NA NA NA NA
>> 49  NA NA NA NA
>> Then the NA�s go down to one numbered 973.  Where did those extras likely 
>> come from, and how do I get rid of them?  I assume I need to get rid of all 
>> the lines after #46,  to do calculations and graphics, no?
>> David
>>  [[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.
> 
> -- 
> Michael
> 
> __
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.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.


[R] lattice densityplot with weights

2023-09-29 Thread Naresh Gurbuxani


density() function in R accepts weights as an input.  Using this
function, one can calculate density and plot it.  Is it possible to
combined these two operations in lattice densityplot()?

mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt =
diff(pnorm(seq(-3, 3, by = 0.2
mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt =
mydf$wt))
with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |>
plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots"))
with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |>
lines(lty = 2, col = 2))
grid()
legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2),
bty = "n")

# I want to do something like this:
# densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l", "g"))

__
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] save(), load(), saveRDS(), and readRDS()

2023-09-29 Thread Ivan Krylov
On Thu, 28 Sep 2023 23:46:45 +0800
Shu Fai Cheung  wrote:

> In my personal work, I prefer using saveRDS() and loadRDS() as I
> don't like the risk of overwriting anything in the global
> environment.

There's the load(file, e <- new.env()) idiom, but that's potentially
a lot to type.

Confusingly, ?save also says:

>> For saving single R objects, ‘saveRDS()’ is mostly preferable to
>> ‘save()’, notably because of the _functional_ nature of ‘readRDS()’,
>> as opposed to ‘load()’.

> The files produced by save
>  have a header
> identifying the file type and so are better protected against
> erroneous use."

This header is also mentioned elsewhere in ?saveRDS:

>> ‘save’ writes a single line header (typically ‘"RDXs\n"’)

The difference between the save() header and the serialize() header is
that the save() header is designed to be read independently from the
machine running the code: it's exactly 5 bytes; some precisely defined
combinations of those 5 bytes identify how the rest of the file should
be interpreted (nowadays, it's likely either "XDR format version 2" or
"XDR format version 3"), and the rest of them cause an error.

The serialize() header does contain enough information describing it
(there's the first byte choosing between ASCII/XDR/native binary and a
number of encoded integers describing the format version and the
version of R you need to parse it), but it's stored in terms of
serialized objects, so if you cannot for some reason decode them
properly, you won't be able to read the header. A little bit of
Catch-22.

> When will the problem mentioned in the warning occur? That is, when
> will a file saved by saveRDS() not be read correctly?

One example I can offer is when a dataset is saved using serialize(xdr
= FALSE) (which is not reachable using saveRDS()). The resulting file
format would be dependent on the native byte order of the CPU in your
computer. (Nowadays it's really hard to encounter a CPU that doesn't
use little-endian byte order, so this is doubly unlikely to happen in
practice.) Both save() and saveRDS() set xdr = TRUE and convert the
data to "network byte order" (big-endian) when saving and back - when
loading.

The warning is relatively fresh (May 2021). Perhaps Prof. Brian D.
Ripley (who made that change) will be able to explain it better.

-- 
Best regards,
Ivan

__
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: [ESS] Have M-x R point at the most recent version of R

2023-09-29 Thread Manuel Teodoro via ESS-help
ESS needs C:/Programme/R/R-4.2.2/bin/R.exe

I'm not sure if I'm missing something but it seems to me that you want to
define it in your init file, but then you will have to update it each time
that you change R versions. Use the following line

(setq inferior-ess-r-program "C:/Users/manuel/Bin/R-4.2.2/bin/R.exe")

Change the path for whichever path/version you have. For example, I am
developing some software under version 4.1.2 so, whenever I need to switch
to that I just write in the *scratch* buffer (it works from anywhere
actually) the following

(setq inferior-ess-r-program "C:/Users/manuel/Bin/R-4.1.2/bin/R.exe")

Stand at the very end of the line, C-x C-e to evaluate the expression and
my next R session. Since I don't do it often I just write and evaluate the
line. But it would be easy to create a function that switches versions as
you command it.

For me this is easier than allowing ESS to find it because I don't have it
installed in the default path due to admin restrictions. I hope it can help
you.

Manuel

On Wed, Sep 27, 2023 at 8:30 AM Hüsing, Johannes via ESS-help <
ess-help@r-project.org> wrote:

> Thanks for the hint.
>
> The path variable is now
> %USERPROFILE%\AppData\Local\Microsoft\WindowsApps;%USERPROFILE%\AppData\Local\Programs\MiKTeX\miktex\bin\x64;C:\Program
> Files\R\R-4.3.1\bin;
>
> M-x R will still start R-4.2.2. After renaming C:\Programme\R\R-4.2.2, M-x
> R will lead to Emacs displaying the message:
> C:/Programme/R/R-4.2.2/bin/R program not found
>
> Is there a way to proceed to notify ESS about the new version installed
> and have it start by default?
>
>
>
> Johannes Hüsing
> Epidemiologie
>
>
> Landeskrebsregister NRW gGmbH
> Gesundheitscampus 10
> 44801 Bochum
> T 0234 54509-216
> F 0234 54509-499
> johannes.hues...@krebsregister.nrw.de
> www.landeskrebsregister.nrw.de
>
>
> 1. Landesqualitätskonferenz jetzt anmelden unter Qualitätskonferenzen
> 
> Newsletter LKR NRW jetzt anmelden unter Newsletter
> 
> Geschäftsführer
> Dr. Andres Schützendübel
> Vorsitzender der Gesellschafterversammlung
> Staatssekretär Dr. Edmund Heller
> Sitz der Gesellschaft
> Bochum
> Registergericht
> Amtsgericht Bochum
> HRB 17715
>
> HINWEIS: Diese Nachricht ist nur für den Adressaten bestimmt. Es ist nicht
> erlaubt, diese Nachricht zu kopieren oder Dritten zugänglich zu machen.
> Sollten Sie irrtümlich diese Nachricht erhalten haben, bitte ich um Ihre
> Mitteilung per E-Mail oder unter der oben angegebenen Telefonnummer.
>
>
> -Ursprüngliche Nachricht-
> Von: Toby Hocking 
> Gesendet: Dienstag, 26. September 2023 18:02
> An: Hüsing, Johannes 
> Cc: ess-help@r-project.org
> Betreff: Re: [ESS] Have M-x R point at the most recent version of R
>
> I put it on the path,
> Windows key -> Edit the system environment variables -> Environment
> variables... -> Double click Path -> add C:\Program
> Files\R\R-devel\bin\x64 or whatever
>
> On Tue, Sep 26, 2023 at 3:07 AM Hüsing, Johannes via ESS-help <
> ess-help@r-project.org> wrote:
> >
> > Dear ESS-helping crowd,
> > following the advice on https://r-pkgs.org/setup.html, I installed R
> version 4.3.1 for MS Windows from CRAN.
> >
> > How do I communicate to ESS that a new version has been installed? I
> read the advice on
> https://emacs.stackexchange.com/questions/60681/ess-start-process-does-not-include-new-r-version,
> I used the Emacs customisation dialogue to set ess-rterm-version-paths from
> C:/Program Files/R/R-4.2.2/bin/x64/Rterm.exe to C:/Program
> Files/R/R-4.3.1/bin/x64/Rterm.exe. After that, the variable
> ess-r-runner-prefixes, as well as ess-r-versions resolves to ("R-1" "R-2"
> "R-3" "R-4" "R-5" "R-6" "R-7" "R-devel" "R-patched"), and both M-x R and
> M-x R-newest will start R 4.2.2. The variable ess-r-versions-created
> resolves to (R-4.2.2-64bit), as does its alias ess-r-created-runners.
> Neither of these is customizable in the Emacs dialog.
> >
> > Is there a way to proceed to notify ESS about the new version installed?
> RStudio opened a miniscule dialog window indicating that it detected
> conflicting versions and had me choose one of them.
> >
> > Thanks for any hint.
> >
> >
> >
> > Dr. Johannes Hüsing
> > Epidemiologie
> >
> > Landeskrebsregister NRW gGmbH
> > Gesundheitscampus 10
> > 44801 Bochum
> >
> >
> >
> > T 0234 54509-216
> > F 0234 54509-499
> > johannes.hues...@krebsregister.nrw.de > ister.nrw.de>
> > www.landeskrebsregister.nrw
> >
> > Das Landeskrebsregister NRW online
> > FACEBOOK:
> > www.facebook.com/LKR.NordrheinWestfalen > rdrheinWestfalen>
> > INSTAGRAM: https://www.instagram.com/landeskrebsregister_nrw
> > NEWSLETTER: https://www.landeskrebsregister.nrw/aktuelles/newsletter
> >
> >
> > Geschäftsführer
> > Dr. Andres Schützendübel
> > Vorsitzender der Gesellschafterversammlung Staatssekretär Matthias
> > Heidmeier Sitz der Gesellschaft Bochum Registergericht Amtsgericht
> > Bochum HRB 17715
> >
> >