Re: [R] Save rgl plot3d Graph as Image

2012-06-26 Thread iris
In the rgl package *rgl.postscript *can save 3d scatter plots you have
generated using the plot3d command .

For example
open3d()
  x <- sort(rnorm(1000))
  y <- rnorm(1000)
  z <- rnorm(1000) + atan2(x,y)
  plot3d(x, y, z, col=rainbow(1000))

rgl.postscript("persp3dd.pdf","pdf")

--
View this message in context: 
http://r.789695.n4.nabble.com/Save-rgl-plot3d-Graph-as-Image-tp898351p4634478.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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] running a function repeatedly on error....

2023-04-19 Thread Iris Simmons
I might try something like this:

FUN1 <- function ()
{
threshold <- 4L
fails <- 0L
internal <- function() {
## do the actual downloading here
tryCatch({
download.file(<...>)
}, error = function() {
fails <<- fails + 1L
if (fails >= threshold) stop("unable to download file(s)")
internal()
})
}
internal()
}

which should attempt to download the files, stopping after 4 failed
attempts. I hope this helps!

On Wed, Apr 19, 2023, 12:57 akshay kulkarni  wrote:

> Dear members,
>   I have a function FUN1 that downloads some data
> from the internet. It so happens that the function doesn't work the first
> time, but on the second or third attempt it works. I want to run the
> function repeatedly for four times if it throws an error:
>
> X <- tryCatch(FUN1, error = function(c) {FUN1})
>
> This runs the function two times. But I want to run the function four
> times if throws an error, but on the fifth attempt if it throws an error,
> abort. I know I can include the tryCatch call inside FUN1 and call it, but
> any short and elegant code to that effect?
>
> 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] extract parts of a list before symbol

2023-05-26 Thread Iris Simmons
You probably want `names(test)`.

On Thu, May 25, 2023 at 7:58 PM Evan Cooch  wrote:

> Suppose I have the following list:
>
> test <- list(a=3,b=5,c=11)
>
> I'm trying to figure out how to extract the characters to the left of
> the equal sign (i.e., I want to extract a list of the variable names, a,
> b and c.
>
> I've tried the permutations I know of involving sub - things like
> sub("\\=.*", "", test), but no matter what I try, sub keeps returning
> (3, 5, 11). In other words, even though I'm trying to extract the
> 'stuff' before the = sign, I seem to be successful only at grabbing the
> stuff after the equal sign.
>
> Pointers to the obvious fix? Thanks...
>
> __
> 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] environments: functions within functions

2023-05-26 Thread Iris Simmons
Hi,


I think there are two easy ways to fix this. The first is to use a `switch`
to call the intended function, this should not be a problem since there are
a small number of print functions in **mixR**

```R
print.mixfitEM <- function (x, digits = getOption("digits"), ...)
{
switch(x$family,
gamma   = printgamma  (x, digits),
lnorm   = printlnorm  (x, digits),
normal  = printnormal (x, digits),
weibull = printweibull(x, digits),
stop(gettextf("invalid '%s' value", "x$family", domain = "R")))
invisible(x)
}
environment(print.mixfitEM) <- getNamespace("mixR")
print.mixfitEM <- compiler::cmpfun(print.mixfitEM)
```

This is nice because 'x' is no longer evaluated twice (you could try this
yourself with something like
`mixR:::print.mixfitEM(writeLines("testing"))`, you'll see the output
twice, once for `x$family` and a second for evaluating `match.call()`
expression), it follows standard evaluation, and 'x' is returned invisibly
at the end, like most other `print` methods. If you really wanted to
continue using `eval`, you could instead do something like

```R
print.mixfitEM <- function (x, digits = getOption("digits"), ...)
{
expr <- quote(printfunction(x, digits))
expr[[1L]] <- as.symbol(paste0("print", x$family))
eval(expr)
invisible(x)
}
environment(print.mixfitEM) <- getNamespace("mixR")
print.mixfitEM <- compiler::cmpfun(print.mixfitEM)
```

This also solves the same issues, but it's ugly and slower.

At least for now, I would copy one of the functions above into the
site-wide startup profile file or your user profile, along with

```R
utils::assignInNamespace("print.mixfitEM", print.mixfitEM, "mixR")
```

This does have the unfortunate side effect of loading **mixR** every time
an R session is launched, but you could also put it inside another function
like:

```R
fix.mixR.print.mixfitEM <- function ()
{
print.mixfitEM <- function(x, digits = getOption("digits"), ...) {
switch(x$family,
gamma   = printgamma  (x, digits),
lnorm   = printlnorm  (x, digits),
normal  = printnormal (x, digits),
weibull = printweibull(x, digits),
stop(gettextf("invalid '%s' value", "x$family", domain = "R")))
invisible(x)
}
environment(print.mixfitEM) <- getNamespace("mixR")
print.mixfitEM <- compiler::cmpfun(print.mixfitEM)
utils::assignInNamespace("print.mixfitEM", print.mixfitEM, "mixR")
}
```

which you would then call in your scripts before using **mixR**. I hope
this helps!

On Thu, May 25, 2023 at 10:19 AM Sarah Goslee 
wrote:

> Hi,
>
> I ran into a problem with S3 method dispatch and scoping while trying
> to use functions from the mixR package within my own functions. I know
> enough to find the problem (I think!), but not enough to fix it
> myself. The problem isn't really a package-specific problem, so I'm
> starting here, and will file an issue with the maintainer once I have
> a solution.
>
> Detailed explanation below, but briefly, the S3 methods in this
> package use match.call() and then eval() to select the correct
> internal method. This works fine from the command line, but if the
> method is called from within another function, the use of
> environment() within eval() means that the objects passed to the
> wrapper function are no longer visible within the eval() call.
>
> I have a two-part question:
> A. How do I get around this right now?
> B. What would the correct approach be for the package authors?
>
> library(mixR)
>
> # first example from ?mixfit
> ## fitting the normal mixture models
> set.seed(103)
> x <- rmixnormal(200, c(0.3, 0.7), c(2, 5), c(1, 1))
> data <- bin(x, seq(-1, 8, 0.25))
> fit1 <- mixfit(x, ncomp = 2)  # raw data
> rm(x, data)
> ###
>
> # simple function
> funworks <- function(x) {
> print(x)
> }
>
> ###
>
> # almost identical simple function
> funfails <- function(thisx) {
> print(thisx)
> }
>
> ###
>
> funworks(fit1)
> funfails(fit1)
>
> ###
>
> The explanation as I understand it...
>
> print called on this object gets passed to print.mixfitEM(), which is:
>
>
> function (x, digits = getOption("digits"), ...)
> {
> family <- x$family
> mc <- match.call()
> mc$digits <- digits
> fun.name <- paste0("print", family)
> mc[[1]] <- as.name(fun.name)
> eval(mc, environment())
> }
>
>
> Working through the calls, when eval() is called from within funfails(),
> mc is
> printnormal(x = thisx, digits = 7)
> and the calling environment does not contain thisx.
>
> In funworks(), it's
> printnormal(x = x, digits = 7)
>
> and x is found.
>
> So, I can get around the problem by naming my argument x, as in
> funworks(), but that's unsatisfying. Is there something else I can do
> to get my functions to work?
>
> And what's the correct way to do what print.mixfitEM() is doing, so
> that it works regardless? I poked around for a while, but didn't find
> a clear (to me!) answer.
>
> Thanks,
> Sarah
>
> --
> Sarah Goslee (she/her)
> http://www.num

[R] Line Directives in Sweave Document

2023-06-28 Thread Iris Simmons
Hello,


I'm trying to demonstrate the behaviour of my R package and how line
directives change that behaviour. So I've got an R chunk like this:

<>=
{
#line 1 "file1.R"
fun <- function() {
pkg::fun()
}


#line 1 "file2.R"
fun()
}
@

but when it is rendered, the line directives do not appear:

> {
+ fun <- function() {
+ pkg::fun()
+ }
+
+
+ fun()
+ }

I tried faking a line directive by writing it twice:

<>=
{
 #line 1 "file1.R"
#line 1 "file1.R"
fun <- function() {
pkg::fun()
}


 #line 1 "file2.R"
#line 1 "file2.R"
fun()
}
@

but then the rendered fake line directive looks bad because it's out
by one space so if it is copy-pasted, it wouldn't actually function as
shown.

So is there an option I can set to have the line directives displayed,
or something else I can change to get it working? Thank you!

__
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] replacing unicode characters

2023-06-28 Thread Iris Simmons
Hiya!


You can do this by specifying sub="c99" instead of "Unicode":

```R
x <- "fa\xE7ile"
xx <- iconv(x, "latin1", "UTF-8")
iconv(xx, "UTF-8", "ASCII", "c99")
```

produces:

```
> x <- "fa\xE7ile"
> xx <- iconv(x, "latin1", "UTF-8")
> iconv(xx, "UTF-8", "ASCII", "c99")
[1] "fa\\u00e7ile"
>
```

For future reference, you can find this in section Examples of the
help page ?iconv
I hope this helps!

On Wed, Jun 28, 2023 at 3:09 PM Adrian Dușa  wrote:
>
> Dear list,
>
> Building on the example from ?iconv:
> x <- "fa\xE7ile"
> xx <- iconv(x, "latin1", "UTF-8") # "façile"
>
> and:
>
> iconv(xx, "UTF-8", "ASCII", "Unicode")
> # "faile"
>
> This is the type of result I sometimes get from an R script that I cannot
> reproduce here, because it depends on a terminal process started in a
> compiled Electron (Node.js) application, under MacOS.
>
> I was wondering, is there a standard way, perhaps also using iconv(), to
> convert this type of result to a more manageable unicode representation?
>
> Something like: "fa\u00e7ile"
>
> Or perhaps a clever regexp, for any number of such occurrences in a string?
>
> Thanks a lot in advance,
> Adrian
>
> [[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] Stacking matrix columns

2023-08-05 Thread Iris Simmons
You could also do

dim(x) <- c(length(x), 1)

On Sat, Aug 5, 2023, 20:12 Steven Yen  wrote:

> I wish to stack columns of a matrix into one column. The following
> matrix command does it. Any other ways? Thanks.
>
>  > x<-matrix(1:20,5,4)
>  > x
>   [,1] [,2] [,3] [,4]
> [1,]16   11   16
> [2,]27   12   17
> [3,]38   13   18
> [4,]49   14   19
> [5,]5   10   15   20
>
>  > matrix(x,ncol=1)
>[,1]
>   [1,]1
>   [2,]2
>   [3,]3
>   [4,]4
>   [5,]5
>   [6,]6
>   [7,]7
>   [8,]8
>   [9,]9
> [10,]   10
> [11,]   11
> [12,]   12
> [13,]   13
> [14,]   14
> [15,]   15
> [16,]   16
> [17,]   17
> [18,]   18
> [19,]   19
> [20,]   20
>  >
>
> __
> 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] Numerical stability of: 1/(1 - cos(x)) - 2/x^2

2023-08-15 Thread Iris Simmons
You could rewrite

1 - cos(x)

as

2 * sin(x/2)^2

and that might give you more precision?

On Wed, Aug 16, 2023, 01:50 Leonard Mada via R-help 
wrote:

> Dear R-Users,
>
> I tried to compute the following limit:
> x = 1E-3;
> (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x)
> # 0.4299226
> log(2)/2 + 1/12
> # 0.4299069
>
> However, the result diverges as x decreases:
> x = 1E-4
> (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x)
> # 0.9543207
> # correct: 0.4299069
>
> I expected the precision to remain good with x = 1E-4 or x = 1E-5.
>
> This part blows up - probably some significant loss of precision of
> cos(x) when x -> 0:
> 1/(cos(x) - 1) - 2/x^2
>
> Maybe there is some room for improvement.
>
> Sincerely,
>
> Leonard
> ==
> The limit was part of the integral:
> up = pi/5;
> integrate(function(x) 1 / sin(x)^3 - 1/x^3 - 1/(2*x), 0, up)
> (log( (1 - cos(up)) / (1 + cos(up)) ) +
>  + 1/(cos(up) - 1) + 1/(cos(up) + 1) + 2*log(2) - 1/3) / 4 +
>  + (1/(2*up^2) - log(up)/2);
>
> # see:
>
> https://github.com/discoleo/R/blob/master/Math/Integrals.Trig.Fractions.Poly.R
>
> __
> 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] Numerical stability of: 1/(1 - cos(x)) - 2/x^2

2023-08-16 Thread Iris Simmons
You might also be able to rewrite

log(1 - cos(x))

as

log1p(-cos(x))

On Wed, Aug 16, 2023, 02:51 Iris Simmons  wrote:

> You could rewrite
>
> 1 - cos(x)
>
> as
>
> 2 * sin(x/2)^2
>
> and that might give you more precision?
>
> On Wed, Aug 16, 2023, 01:50 Leonard Mada via R-help 
> wrote:
>
>> Dear R-Users,
>>
>> I tried to compute the following limit:
>> x = 1E-3;
>> (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x)
>> # 0.4299226
>> log(2)/2 + 1/12
>> # 0.4299069
>>
>> However, the result diverges as x decreases:
>> x = 1E-4
>> (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x)
>> # 0.9543207
>> # correct: 0.4299069
>>
>> I expected the precision to remain good with x = 1E-4 or x = 1E-5.
>>
>> This part blows up - probably some significant loss of precision of
>> cos(x) when x -> 0:
>> 1/(cos(x) - 1) - 2/x^2
>>
>> Maybe there is some room for improvement.
>>
>> Sincerely,
>>
>> Leonard
>> ==
>> The limit was part of the integral:
>> up = pi/5;
>> integrate(function(x) 1 / sin(x)^3 - 1/x^3 - 1/(2*x), 0, up)
>> (log( (1 - cos(up)) / (1 + cos(up)) ) +
>>  + 1/(cos(up) - 1) + 1/(cos(up) + 1) + 2*log(2) - 1/3) / 4 +
>>  + (1/(2*up^2) - log(up)/2);
>>
>> # see:
>>
>> https://github.com/discoleo/R/blob/master/Math/Integrals.Trig.Fractions.Poly.R
>>
>> __
>> 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] Create a call but evaluate only some elements

2023-10-25 Thread Iris Simmons
You can try either of these:

expr <- bquote(lm(.(as.formula(mod)), dat))
lm_out5 <- eval(expr)

expr <- call("lm", as.formula(mod), as.symbol("dat"))
lm_out6 <- eval(expr)

but bquote is usually easier and good enough.

On Wed, Oct 25, 2023, 05:10 Shu Fai Cheung  wrote:

> Hi All,
>
> I have a problem that may have a simple solution, but I am not
> familiar with creating calls manually.
>
> This is example calling lm()
>
> ``` r
> set.seed(1234)
> n <- 10
> dat <- data.frame(x1 = rnorm(n),
>   x2 = rnorm(n),
>   y = rnorm(n))
>
> lm_out <- lm(y ~ x1 + x2, dat)
> lm_out
> #>
> #> Call:
> #> lm(formula = y ~ x1 + x2, data = dat)
> #>
> #> Coefficients:
> #> (Intercept)   x1   x2
> #> -0.5755  -0.4151  -0.2411
> lm_out$call
> #> lm(formula = y ~ x1 + x2, data = dat)
> ```
>
> The call is stored, "lm(formula = y ~ x1 + x2, data = dat)", and names
> are not evaluated.
>
> I want to create a similar call, but only one of the elements is from a
> string.
>
> ```r
> mod <- "y ~ x1 + x2"
> ```
>
> This is what I tried but failed:
>
> ```r
> lm_out2 <- do.call("lm",
>list(formula = as.formula(mod),
> data = dat))
> lm_out2
> #>
> #> Call:
> #> lm(formula = y ~ x1 + x2, data = structure(list(x1 =
> c(-1.20706574938542,
> #> 0.27742924211066, 1.08444117668306, -2.34569770262935, 0.42912468881105,
> #> 0.506055892157574, -0.574739960134649, -0.546631855784187,
> -0.564451999093283,
> #> -0.890037829044104), x2 = c(-0.477192699753547, -0.998386444859704,
> #> -0.77625389463799, 0.0644588172762693, 0.959494058970771,
> -0.110285494390774,
> #> -0.511009505806642, -0.911195416629811, -0.83717168026894,
> 2.41583517848934
> #> ), y = c(0.134088220152031, -0.490685896690943, -0.440547872353227,
> #> 0.459589441005854, -0.693720246937475, -1.44820491038647,
> 0.574755720900728,
> #> -1.02365572296388, -0.0151383003641817, -0.935948601168394)), class
> = "data.frame", row.names = c(NA,
> #> -10L)))
> #>
> #> Coefficients:
> #> (Intercept)   x1   x2
> #> -0.5755  -0.4151  -0.2411
> ```
>
> It does not have the formula, "as a formula": y ~ x1 + x2.
> However, the name "dat" is evaluated. Therefore, the call stored does
> not have the name 'dat', but has the evaluated content.
>
> The following fits the same model. However, the call stores the name,
> 'mod', not the evaluated result, y ~ x1 + x2.
>
> ```r
> lm_out3 <- lm(mod, data = dat)
> lm_out3
> #>
> #> Call:
> #> lm(formula = mod, data = dat)
> #>
> #> Coefficients:
> #> (Intercept)   x1   x2
> #> -0.5755  -0.4151  -0.2411
> ```
>
> The following method works. However, I have to do a dummy call,
> extract the stored call, and set formula to the result of
> as.formula(mod):
>
> ```r
> lm_out3 <- lm(mod, data = dat)
> lm_out3
> #>
> #> Call:
> #> lm(formula = mod, data = dat)
> #>
> #> Coefficients:
> #> (Intercept)   x1   x2
> #> -0.5755  -0.4151  -0.2411
>
> call1 <- lm_out3$call
> call1$formula <- as.formula(mod)
> lm_out4 <- eval(call1)
> lm_out4
> #>
> #> Call:
> #> lm(formula = y ~ x1 + x2, data = dat)
> #>
> #> Coefficients:
> #> (Intercept)   x1   x2
> #> -0.5755  -0.4151  -0.2411
> ```
>
> Is it possible to create the call directly, with only 'mod' evaluated,
> and other arguments, e.g., 'dat', not evaluated?
>
> Regards,
> Shu Fai
>
> __
> 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] Bug in print for data frames?

2023-10-26 Thread Iris Simmons
I would say this is not an error, but I think what you wrote isn't
what you intended to do anyway.

y[1] is a data.frame which contains only the first column of y, which
you assign to x$C, so now x$C is a data.frame.

R allows data.frame to be plain vectors as well as matrices and
data.frames, basically anything as long as it has the correct length
or nrow.

When the data.frame is formatted for printing, each column C is
formatted then column-bound into another data.frame using
as.data.frame.list, so it takes the name A because that's the name of
the column from y.

I think what you meant to do is x$C <- y[[1]]  ## double brackets
instead of single

On Thu, Oct 26, 2023 at 4:14 AM Christian Asseburg  wrote:
>
> Hi! I came across this unexpected behaviour in R. First I thought it was a 
> bug in the assignment operator <- but now I think it's maybe a bug in the way 
> data frames are being printed. What do you think?
>
> Using R 4.3.1:
>
> > x <- data.frame(A = 1, B = 2, C = 3)
> > y <- data.frame(A = 1)
> > x
>   A B C
> 1 1 2 3
> > x$B <- y$A # works as expected
> > x
>   A B C
> 1 1 1 3
> > x$C <- y[1] # makes C disappear
> > x
>   A B A
> 1 1 1 1
> > str(x)
> 'data.frame':   1 obs. of  3 variables:
>  $ A: num 1
>  $ B: num 1
>  $ C:'data.frame':  1 obs. of  1 variable:
>   ..$ A: num 1
>
> Why does the print(x) not show "C" as the name of the third element? I did 
> mess up the data frame (and this was a mistake on my part), but finding the 
> bug was harder because print(x) didn't show the C any longer.
>
> Thanks. With best wishes -
>
> . . . Christian
>
> __
> 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] How to Reformat a dataframe

2023-10-27 Thread Iris Simmons
You are not getting the structure you want because the indexes are
wrong. They should be something more like this:

i <- 0
for (row in 1:nrow(alajuela_df)){
  for (col in 1:ncol(alajuela_df)){
i <- i + 1
df[i,1]=alajuela_df[row,col]
  }
}

but I think what you are doing can be written much shorter and will run faster:

## transpose here matches your original code
df <- data.frame(aportes_alajuela = c(t(alajuela_df)))

## but if you do not want to transpose, then do this
df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names = FALSE))

However, you said you expected 1509 observations, but this gives you
1512 observations. If you want to exclude the 3 NA observations, do
something like:

df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]

On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal  wrote:
>
> Dear friends,
>
> I have the following dataframe:
> dim(alajuela_df)
> [1] 126  12
>
> dput(alajuela_df)
> structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
> 28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
> 19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
> 46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
> 30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
> 122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
> 54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
> 64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
> 82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
> 32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
> 50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
> 32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
> 88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
> 35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
> 40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
> 33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
> 36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
> 29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
> 27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
> 53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
> 36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
> 47.5373940344086, 62.5177012273297, 31.970248036, 29.6637272685484,
> 49.9578249978495, 26.1936613831541, 30.8051128442652, 153.165249601165,
> 36.7652008047491, 14.3854334390681, 55.9930862447133, 87.5882628859319,
> 88.414763059767, 58.6046644034946, 20.5301898890681, 39.0037205346774,
> 67.5971386298632, 25.1295804696237, 32.7864973072581, 59.5662701215054,
> 56.3503435331631, 25.2768483884409, 120.187620533964, 51.3699688189953,
> 42.6889983487868, 34.5183987020052, 32.9513272936733, 20.6141437461215,
> 43.4197954932283, 107.258904254306, 23.7548470509806, 48.4457090643702,
> 72.3249287581108, 32.7272590911596, 48.6115621286089), ...2 = c(34.9996266,
> 22.00219245, 22.9932822, 19.00060635, 35.99071635, 23.9560551,
> 44.99547465, 22.00219245, 17.35822905, 38.9639856, 16.4804067,
> 93.87035775, 81.2693595, 65.4119235, 14.6114946, 33.32893245,
> 23.3330844, 66.68618175, 29.1097218, 18.77407155, 28.1469489,
> 20.64298365, 15.31941585, 25.79665035, 39.5869563, 35.9057658,
> 28.11863205, 27.09922545, 18.03783345, 52.3295388, 47.99706075,
> 21.88892505, 18.8590221, 17.64139755, 29.2796229, 27.750513,
> 26.9576412, 36.8932410314484, 19.1194611744253, 37.5166237491071,
> 37.9625421619439, 23.444984650496, 37.4408609345785, 43.1033210084325,
> 28.1252430606151, 38.4216392645833, 41.0532302780651, 34.4822260610119,
> 27.8096188059524, 25.9720387760913, 19.5989670673372, 21.1366557174603,
> 34.0091174203373, 111.149568174301, 24.5438738482759, 62.923693488747,
> 36.056976280754, 36.934798630754, 47.0895472967433, 23.0987961767857,
> 45.1991799260913, 23.0274422095238, 26.8716961246169, 21.5009916962302,
> 25.315076619246, 41.2098751296627, 15.6537752838123, 20.1723471152778,
> 31.7437694203373, 34.6486999232143, 27.6318028338123, 23.9637897951389,
> 50.6646878095238, 33.3208231933532, 38.0673028746602, 19.9469359998016,
> 27.0503264451389, 19.838575356, 29.0062549955939, 17.231889453373,
> 25.332044256, 17.651735447619, 37.12988188841, 37.7982565469246,
> 33.1890524548611, 15.4283008803571, 33.5040593288314, 24.9726150325397,
> 29.0612348127976, 30.2629592267857, 32.8065534719349, 43.3676303365079,
> 39.19334780424, 23.9252474159722, 15.8641806903257, 27.51501019685,
> 21.5861832758362, 18.5136698379577, 54.9166790494253, 29.2865335598214,
> 12.8130786005824, 46.2078437446569, 37.9351942968391, 31.8758398080357,
> 25.483024048, 17.4062173291544, 19.1528843310516, 37.2160194109127,
> 25.3577400835684, 21.3381885368056, 37.5842732179795, 62.4856267757438,
> 19.2393143990079, 49.4806272500855, 29.5274325254668, 18.7819282827049,

Re: [R] Dynamically create a (convenience) function in a package

2023-10-30 Thread Iris Simmons
If you don't know the name of the attributes in advance, how can you know
the function name to be able to call it? This seems like a very flawed
approach.

Also, I would discourage the use of eval(parse(text = )), it's almost
always not the right way to do what you want to do. In your case,

eval(bquote(function(x) attr(x, .(n

would be better.


On Mon, Oct 30, 2023, 06:28 Sigbert Klinke 
wrote:

> Hi,
>
> n a package, I have a data object with attributes, and I want to
> dynamically create a convenience function to access those attributes.
> This way, instead of using attr(x, "number"), I would like to use
> number(x).
>
>
>
> Because I don't know in advance which attributes the data object may
> have, I've used the following algorithm:
>
> x <- structure(pi, number=exp(1))
>
> a <- attributes(x)
>
> for (n in names(a)) {
>
>if (!exists(n, mode="function")) {
>
>  f <- eval(parse(text=sprintf("function(x) { attr(x, '%s') } ", n)))
>
>
>  assign(n, f, envir=.GlobalEnv)
>
>}
>
> }
>
> number(x)
>
> However, I believe modifying the global environment with this is not
> allowed by CRAN for a package. Is there a way to implement such
> functionality?
>
> Thanks Sigbert
>
> --
> https://hu.berlin/sk
> https://www.stat.de/faqs
> https://hu.berlin/mmstat
> https://hu.berlin/mmstat-ar
>
> __
> 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] Calling Emacs Lisp Code/Function from R

2023-11-10 Thread Iris Simmons
Hi,


I'm using R in Emacs and I'm interested in programatically knowing the
details of all opened buffers; details such a buffer name, size, mode,
and possibly associated filename. I've been able to write such a
function in Emacs Lisp, but now I'd like to be able to call that
function from R, or if that's not possible then calling it from C
would be fine.

Does anyone know if this is possible? And if so, could you perhaps
direct me towards an existing R package or program that calls an Emacs
Lisp function from R that I could use as a guide?


Thank you!
Iris

__
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] Calling Emacs Lisp Code/Function from R

2023-11-12 Thread Iris Simmons
Hi,


Thank you Duncan, I will check with that other mailing list to see if
they can guide me.
And thank you Martin, I was able to implement what I wanted using the
example you sent.

On Fri, Nov 10, 2023 at 6:55 AM Martin Gregory via R-help
 wrote:
>
> Hi,
>
> if you run a server in your Emacs session you can use emacsclient to
> send a lisp call to the server. There's an example here:
>
> https://emacs.stackexchange.com/questions/54156/how-can-i-query-emacs-from-a-separate-process/54161#54161
>
> Regards,
> Martin Gregory
>
> On 11/10/23 11:18, Duncan Murdoch wrote:
> > I'm not an Emacs user, but the ESS-help mailing list (see
> > ess.r-project.org) might be able to help with this.
> >
> > Duncan Murdoch
> >
> > On 10/11/2023 3:43 a.m., Iris Simmons wrote:
> >> Hi,
> >>
> >>
> >> I'm using R in Emacs and I'm interested in programatically knowing the
> >> details of all opened buffers; details such a buffer name, size, mode,
> >> and possibly associated filename. I've been able to write such a
> >> function in Emacs Lisp, but now I'd like to be able to call that
> >> function from R, or if that's not possible then calling it from C
> >> would be fine.
> >>
> >> Does anyone know if this is possible? And if so, could you perhaps
> >> direct me towards an existing R package or program that calls an Emacs
> >> Lisp function from R that I could use as a guide?
> >>
> >>
> >> Thank you!
> >>  Iris
> >>
> >> __
> >> 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-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] Capturing Function Arguments

2024-02-18 Thread Iris Simmons
Hi Reed,


I need to stress before giving my answer that no solution can handle
everything. These scenarios will always lead to problems:

* if any of the formal arguments rely on the current state of the call stack

* if any of the formal arguments rely on a variable that is only
defined later in the body of the function

That being said, this function does well to handle other scenarios:

```R
f <- function (...)
{
## replace f0 as needed
wrapped_function <- f0
call <- match.call(wrapped_function, expand.dots = FALSE)
args <- as.list(call)[-1L]
e <- new.env(parent = environment(wrapped_function))
parent_frame <- parent.frame()
formal_args <- formals(wrapped_function)
for (sym in names(formal_args)) {
## if the formal argument is one of the provided arguments
if (i <- match(sym, names(args), 0L)) {
## if the argument is missing, assign as is
if (identical(args[[i]], quote(expr = )))
e[[sym]] <- quote(expr = )
## if the argument is not ..., assign as a promise
else if (sym != "...")
eval(call("delayedAssign", quote(sym), args[[i]],
eval.env = quote(parent_frame), assign.env = quote(e)))
else {
## handle ... separately
## create a variable corresponding to each dot argument,
## then capture them with get("...")
dots <- args[[i]]
for (i in seq_along(dots)) {
sym <- paste0("dd", i)
eval(call("delayedAssign", quote(sym), dots[[i]],
eval.env = quote(parent_frame)))
dots[[i]] <- as.symbol(sym)
}
e[["..."]] <- eval(as.call(c(function(...) get("..."), dots)))
}
}
else {
## similar to above, but this time evaluate in e not parent_frame
i <- match(sym, names(formal_args), 0L)
if (identical(formal_args[[i]], quote(expr = )))
e[[sym]] <- quote(expr = )
else eval(call("delayedAssign", quote(sym),
formal_args[[i]], eval.env = quote(e), assign.env = quote(e)))
}
}
## you don't need to turn into a list, but you can
args2 <- as.list(e, all.names = TRUE)
list(call = call, args = args, args2 = args2, e = e)
## do whatever else you want to here
}
```

in the test scenario you described, it works:

```R
f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
a <- 1
x <- f(a, z = 1 + 100)
x$args2
```

produces:

```
> f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
> a <- 1
> x <- f(a, z = 1 + 100)
> x$args2
$x
[1] 1

$y
[1] 202

$z
[1] 101

$a
NULL

$b


>
```

Regards,
Iris

On Sun, Feb 18, 2024 at 3:51 AM Reed A. Cartwright
 wrote:
>
> I'm wrapping a function in R and I want to record all the arguments
> passed to it, including default values and missing values. I want to
> be able to snoop on function calls in sourced scripts as part of a
> unit testing framework.
>
> I can capture the values fine, but I'm having trouble evaluating them
> as if `force()` had been applied to each of them.
>
> Here is a minimal example:
>
> f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
>
> f <- function(...) {
>   call <- rlang::call_match(fn = f0, defaults = TRUE)
>   args <- rlang::call_args(call)
>   # do something here to evaluate args as if force() had been called
>   # I've tried many things but haven't found a solution that handled 
> everything
>   args
> }
>
> # In the below example args1 and args2 should be the same
> a <- 1
> args1 <- f(a, z = 1 + 100)
>
> args2 <- list( a = 1, y = 202, z = 101, a = NULL, b = rlang::missing_arg() )
>
> If anyone knows how to get this to work, I would appreciate the help.
>
> Thanks,
> Reed
>
> --
> Reed A. Cartwright, PhD
> Associate Professor of Genomics, Evolution, and Bioinformatics
> School of Life Sciences and The Biodesign Institute
> Arizona State University
> ==
> Address: The Biodesign Institute, PO Box 876401, Tempe, AZ 85287-6401 USA
> Packages: The Biodesign Institute, 1001 S. McAllister Ave, Tempe, AZ
> 85287-6401 USA
> Office: Biodesign B-220C, 1-480-965-9949
> Website: http://cartwrig.ht/
>
> __
> 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] gsub issue with consecutive pattern finds

2024-03-01 Thread Iris Simmons
Hi Iago,


This is not a bug. It is expected. Patterns may not overlap. However, there
is a way to get the result you want using perl:

```R
gsub("([aeiouAEIOU])(?=[aeiouAEIOU])", "\\1_", "aerioue", perl = TRUE)
```

The specific change I made is called a positive lookahead, you can read
more about it here:

https://www.regular-expressions.info/lookaround.html

It's a way to check for a piece of text without consuming it in the match.

Also, since you don't care about character case, it might be more legible
to add ignore.case = TRUE and remove the upper case characters:

```R
gsub("([aeiou])(?=[aeiou])", "\\1_", "aerioue", perl = TRUE, ignore.case =
TRUE)

## or

gsub("(?i)([aeiou])(?=[aeiou])", "\\1_", "aerioue", perl = TRUE)
```

I hope this helps!


On Fri, Mar 1, 2024, 06:37 Iago Giné Vázquez  wrote:

> Hi all,
>
> I tested next command:
>
> gsub("([aeiouAEIOU])([aeiouAEIOU])", "\\1_\\2", "aerioue")
>
> with the following output:
>
> [1] "a_eri_ou_e"
>
> So, there are two consecutive vowels where an underscore is not added.
>
> May it be a bug? Is it expected (bug or not)? Is there any chance to get
> what I want (an underscore between each pair of consecutive vowels)?
>
>
> Thank you!
>
> Best regards,
>
> Iago
>
> [[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] Problem with R coding

2024-03-12 Thread Iris Simmons
Hi Maria,


I had something similar on my Windows work laptop at some point where the
home directory was something containing non ASCII characters. The easy
solution is to copy said directly from the file explorer into
utils::shortPathName, and then set that as the home directory. In my case,

> writeLines(utils::shortPathName(r"(C:\Users\iris\OneDrive - Organization
Name Non ASCII\Documents)"))
C:\Users\iris\ONEDRI~1\DOCUME~1

and then in the control panel, then user accounts (twice), then Change My
Environment Variables, enter the variable name HOME and the directory that
was produced by R. Now the HOME directory contains all ASCII characters so
everything works as expected.


I hope this helps!



On Tue, Mar 12, 2024, 04:49 Maria Del Mar García Zamora <
mar.garc...@alumnos.uchceu.es> wrote:

> Hello,
>
> This is the error that appears when I try to load library(Rcmdr). I am
> using R version 4.3.3. I have tried to upload the packages, uninstall them
> and intalling them again and nothing.
> Loading required package: splines
> Loading required package: RcmdrMisc
> Loading required package: car
> Loading required package: carData
> Loading required package: sandwich
> Loading required package: effects
> lattice theme set by effectsTheme()
> See ?effectsTheme for details.
> Error: package or namespace load failed for ‘Rcmdr’:
>  .onLoad failed in loadNamespace() for 'tcltk2', details:
>   call: file.exists("~/.Rtk2theme")
>   error: file name conversion problem -- name too long?
>
> Once this appears I use path.expand('~') and this is R's answer:
> [1] "C:\\Users\\marga\\OneDrive - Fundaci\xf3n Universitaria San Pablo
> CEU\\Documentos"
>
> The thing is that in spanish we use accents, so this word (Fundaci\xf3n)
> really is Fundación, but I can't change it.
>
> I have tried to start R from CDM using: C:\Users\marga>set
> R_USER=C:\Users\marga\R_USER
>
> C:\Users\marga>"C:\Users\marga\Desktop\R-4.3.3\bin\R.exe" CMD Rgui
>
> At the beginning this worked but right now a message saying that this app
> cannot be used and that I have to ask the software company (photo attached)
>
> What should I do?
>
> Thanks,
>
> Mar
>
>
> [https://www.uchceu.es/img/externos/correo/ceu_uch.gif]<
> https://www.uchceu.es/>
>
> Maria Del Mar García Zamora
> Alumno UCHCEU -
> Universidad CEU Cardenal Herrera
> -
> Tel.
> www.uchceu.es<https://www.uchceu.es/>
>
> [https://www.uchceu.es/img/logos/wur.jpg]
> [https://www.uchceu.es/img/externos/correo/medio_ambiente.gif] Por favor,
> piensa en el medio ambiente antes de imprimir este contenido
>
>
>
> [http://www.uchceu.es/img/externos/correo/ceu_uch.gif]<
> http://www.uchceu.es/>
>
> Maria Del Mar García Zamora
>
> www.uchceu.es<http://www.uchceu.es/>
>
> [http://www.uchceu.es/img/externos/correo/medio_ambiente.gif] Por favor,
> piensa en el medio ambiente antes de imprimir este contenido
>
>
>
>
> Este mensaje y sus archivos adjuntos, enviados desde FUNDACIÓN
> UNIVERSITARIA SAN PABLO-CEU, pueden contener información confidencial y
> está destinado a ser leído sólo por la persona a la que va dirigido, por lo
> que queda prohibida la difusión, copia o utilización de dicha información
> por terceros. Si usted lo recibiera por error, por favor, notifíquelo al
> remitente y destruya el mensaje y cualquier documento adjunto que pudiera
> contener. Cualquier información, opinión, conclusión, recomendación, etc.
> contenida en el presente mensaje no relacionada con la actividad de
> FUNDACIÓN UNIVERSITARIA SAN PABLO-CEU, y/o emitida por persona no
> autorizada para ello, deberá considerarse como no proporcionada ni aprobada
> por FUNDACIÓN UNIVERSITARIA SAN PABLO-CEU, que pone los medios a su alcance
> para garantizar la seguridad y ausencia de errores en la correspondencia
> electrónica, pero no puede asegurar la inexistencia de virus o la no
> alteración de los documentos transmitidos electrónicamente, por lo que
> declina cualquier responsabilidad a este respecto.
>
> This message and its attachments, sent from FUNDACIÓN UNIVERSITARIA SAN
> PABLO-CEU, may contain confidential information and is intended to be read
> only by the person it is directed. Therefore any disclosure, copying or use
> by third parties of this information is prohibited. If you receive this in
> error, please notify the sender and destroy the message and any attachments
> may contain. Any information, opinion, conclusion, recommendation,...
> contained in this message and which is unrelated to the business activity
> of FUNDACIÓN UNIVERSITARIA SAN PABLO-CEU and/or issued by unauthorized
> personnel, shall be considered unapprov

Re: [R] as.complex()

2024-03-25 Thread Iris Simmons
Hi Thomas,


If you want to compare the imaginary portions, you could do:

Im(z1) < Im(z2)

If you want to compare the magnitudes, you could do:

Mod(z1) < Mod(z2)

If you want to compare complex numbers, i.e. z1 < z2, well that just
doesn't make sense.

On Mon, Mar 25, 2024, 10:17 Thomas K  wrote:

> Needing a < , > comparison for imaginary numbers
>
> __
> 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] Regexp pattern but fixed replacement?

2024-04-11 Thread Iris Simmons
Hi Duncan,


I only know about sub() and gsub().

There is no way to have pattern be a regular expression and replacement be
a fixed string.

Backslash is the only special character in replacement. If you need a
reference, see this file:
https://github.com/wch/r-source/blob/04650eddd6d844963b6d7aac02bd8d13cbf440d4/src/main/grep.c
particularly functions R_pcre_string_adj and wstring_adj. So just double
the backslashes in replacement and you'll be good to go.

On Thu, Apr 11, 2024, 12:36 Duncan Murdoch  wrote:

> I noticed this issue in stringr::str_replace, but it also affects sub()
> in base R.
>
> If the pattern in a call to one of these needs to be a regular
> expression, then backslashes in the replacement text are treated specially.
>
> For example,
>
>gsub("a|b", "\\", "abcdef")
>
> gives "def", not "def" as I wanted.  To get the latter, I need to
> escape the replacement backslashes, e.g.
>
>gsub("a|b", "", "abcdef")
>
> which gives "cdef".
>
> I have two questions:
>
> 1.  Is there a variant on sub or str_replace which allows the pattern to
> be declared as a regular expression, but the replacement to be declared
> as fixed?
>
> 2.  To get what I want, I can double the backslashes in the replacement
> text.  This would do that:
>
> replacement <- gsub("", "", replacement)
>
> Are there any other special characters to worry about besides backslashes?
>
> Duncan Murdoch
>
> __
> 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] strange behavior in base::as.double

2024-05-01 Thread Iris Simmons
This happens because "123e" looks like exponential form. This string has no
exponent, so it gets treated as 0 exponent.

If you're interested in converting hex numbers, append 0x:

as.numeric("0x123a")

or use strtoi:

strtoi("123a", 16)

On Wed, May 1, 2024, 15:24 Carl Witthoft  wrote:

> Hello.
> I'm running R 4.4.0 on an iMac Venture 13.5.2 .   There appears to be a bug
> in as.double().
>
> Create a string with a numeric digits followed by a single letter a thru f
> (as tho' it's base 16).
>
> for  K  in (a,b,c,d, and f ) ,  as.double( '123K') returns NA
> but as.double('123e') returns 123 -- or whatever the first digit is.
>
> Please let me know if there are additional tests I can try .
>
>
> thanks
> Carl Witthoft
>
> [[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] I have Problem using the Pipe Command

2024-06-18 Thread Iris Simmons
My guess would be that you're using an older version of R. The pipe was
added in R 4.1. It cannot be used in earlier versions as the syntax is
invalid. You could use the dplyr pipe if you want further back
compatibility.

On Tue, Jun 18, 2024, 12:14 Ogbos Okike  wrote:

> Greetings to everyone and thank you for your readiness to help.
>
> I have problems using the pipe command (|>).
>
> Once I have it in any script on my system, it won't.
>
> The error message will say:
>
> unexpected '>'
> I loaded the two packages below to see if it would handle it. But the
> problem remains.
> library(magrittr)
> library(dplyr)
>
> I searched to see if there was a way to install the command. But I was not
> successful.
>
> Please tell me what to do to be able to use the pipe command on my system.
> I would be grateful for any help.
> Thank you.
>
> Sincerely yours
> Ogbos
>
> [[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] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Iris Simmons
It should be written more like this:

```R
z <- data.frame(a = 1:3, b = letters[1:3])
z |> names() |> _[2] <- "foo"
z
```

Regards,
Iris

On Sat, Jul 20, 2024 at 4:47 PM Bert Gunter  wrote:
>
> With further fooling around, I realized that explicitly assigning my
> last "solution" 'works'; i.e.
>
> names(z)[2] <- "foo"
>
> can be piped as:
>
>  z <- z |>(\(x) "names<-"(x,value = "[<-"(names(x),2,'foo')))()
> > z
>   a foo
> 1 1   a
> 2 2   b
> 3 3   c
>
> This is even awfuller than before. So my query still stands.
>
> -- Bert
>
> On Sat, Jul 20, 2024 at 1:14 PM Bert Gunter  wrote:
> >
> > Nope, I still got it wrong: None of my approaches work.  :(
> >
> > So my query remains: how to do it via piping with |> ?
> >
> > Bert
> >
> >
> > On Sat, Jul 20, 2024 at 1:06 PM Bert Gunter  wrote:
> > >
> > > This post is likely pretty useless;  it is motivated by a recent post
> > > from "Val" that was elegantly answered using Tidyverse constructs, but
> > > I wondered how to do it using base R only. Along the way, I ran into
> > > the following question to which I think my answer (below) is pretty
> > > awful. I would be interested in more elegant base R approaches. So...
> > >
> > > z <- data.frame(a = 1:3, b = letters[1:3])
> > > > z
> > >   a h
> > > 1 1 a
> > > 2 2 b
> > > 3 3 c
> > >
> > > Suppose I want to change the name of the second column of z from 'b'
> > > to 'foo' . This is very easy using nested function syntax by:
> > >
> > > names(z)[2] <- "foo"
> > > > z
> > >   a foo
> > > 1 1   a
> > > 2 2   b
> > > 3 3   c
> > >
> > > Now suppose I wanted to do this using |> syntax, along the lines of:
> > >
> > > z |> names()[2] <- "foo"  ## throws an error
> > >
> > > Slightly fancier is:
> > >
> > > z |> (\(x)names(x)[2] <- "b")()
> > > ## does nothing, but does not throw an error.
> > >
> > > However, the following, which resulted from a more careful read of
> > > ?names works (after changing the name of the second column back to "b"
> > > of course):
> > >
> > > z |>(\(x) "names<-"(x,value = "[<-"(names(x),2,'foo')))()
> > > >z
> > >   a foo
> > > 1 1   a
> > > 2 2   b
> > > 3 3   c
> > >
> > > This qualifies to me as "pretty awful." I'm sure there are better ways
> > > to do this using pipe syntax, so I would appreciate any better
> > > approaches.
> > >
> > > Best,
> > > Bert
>
> __
> 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] grep

2024-08-01 Thread Iris Simmons
You can find more by reading through ?regex as well as Perl documentation
(which you can find online).

On Thu, Aug 1, 2024, 21:11 Steven Yen  wrote:

> Good Morning. Below I like statement like
>
> j<-grep(".r\\b",colnames(mydata),value=TRUE); j
>
> with the \\b option which I read long time ago which Ive found useful.
>
> Are there more or these options, other than ? grep? Thanks.
>
> dstat is just my own descriptive routine.
>
>  > x
>   [1] "age"  "sleep""primary"  "middle"
>   [5] "high" "somewhath""veryh""somewhatm"
>   [9] "verym""somewhatc""veryc""somewhatl"
> [13] "veryl""village"  "married"  "social"
> [17] "agricultural" "communist""minority" "religious"
>  > colnames(mydata)
>   [1] "depression" "sleep"  "female" "village"
>   [5] "agricultural"   "married""communist" "minority"
>   [9] "religious"  "social" "no" "primary"
> [13] "middle" "high"   "veryh" "somewhath"
> [17] "notveryh"   "verym"  "somewhatm" "notverym"
> [21] "veryc"  "somewhatc"  "notveryc" "veryl"
> [25] "somewhatl"  "notveryl"   "age" "village.r"
> [29] "married.r"  "social.r"   "agricultural.r" "communist.r"
> [33] "minority.r" "religious.r""male.r" "education.r"
>  > j<-grep(".r\\b",colnames(mydata),value=TRUE); j
> [1] "village.r"  "married.r"  "social.r" "agricultural.r"
> [5] "communist.r""minority.r" "religious.r" "male.r"
> [9] "education.r"
>  > j<-c(x,j); j
>   [1] "age""sleep"  "primary" "middle"
>   [5] "high"   "somewhath"  "veryh" "somewhatm"
>   [9] "verym"  "somewhatc"  "veryc" "somewhatl"
> [13] "veryl"  "village""married" "social"
> [17] "agricultural"   "communist"  "minority" "religious"
> [21] "village.r"  "married.r"  "social.r" "agricultural.r"
> [25] "communist.r""minority.r" "religious.r" "male.r"
> [29] "education.r"
>  > data<-mydata[j]
>  > cbind(
> +   dstat(subset(data,male.r==1))[,1:2],
> +   dstat(subset(data,male.r==0))[,1:2]
> + )
> Sample statistics (Weighted =  FALSE )
>
> Sample statistics (Weighted =  FALSE )
>
>  Mean Std.dev  Mean Std.dev
> age6.279   0.841 6.055   0.813
> sleep  6.483   1.804 6.087   2.045
> primary0.452   0.498 0.408   0.491
> middle 0.287   0.453 0.176   0.381
> high   0.171   0.377 0.082   0.275
> somewhath  0.522   0.500 0.447   0.497
> veryh  0.254   0.435 0.250   0.433
> somewhatm  0.419   0.493 0.460   0.498
> verym  0.544   0.498 0.411   0.492
> somewhatc  0.376   0.484 0.346   0.476
> veryc  0.593   0.491 0.615   0.487
> somewhatl  0.544   0.498 0.504   0.500
> veryl  0.390   0.488 0.389   0.487
> village0.757   0.429 0.752   0.432
> married0.936   0.245 0.906   0.291
> social 0.538   0.499 0.528   0.499
> agricultural   0.780   0.414 0.826   0.379
> communist  0.178   0.383 0.038   0.190
> minority   0.071   0.256 0.081   0.273
> religious  0.088   0.284 0.102   0.302
> village.r  0.243   0.429 0.248   0.432
> married.r  0.064   0.245 0.094   0.291
> social.r   0.462   0.499 0.472   0.499
> agricultural.r 0.220   0.414 0.174   0.379
> communist.r0.822   0.383 0.962   0.190
> minority.r 0.929   0.256 0.919   0.273
> religious.r0.912   0.284 0.898   0.302
> male.r 1.000   0.000 0.000   0.000
> education.r0.090   0.286 0.334   0.472
>  >
>
> __
> 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] Reading a txt file from internet

2024-09-07 Thread Iris Simmons
That looks like a UTF-16LE byte order mark. Simply open the connection
with the proper encoding:

read.delim(

'https://online.stat.psu.edu/onlinecourses/sites/stat501/files/ch15/employee.txt',
fileEncoding = "UTF-16LE"
)

On Sat, Sep 7, 2024 at 3:57 PM Christofer Bogaso
 wrote:
>
> Hi,
>
> I am trying to the data from
> https://online.stat.psu.edu/onlinecourses/sites/stat501/files/ch15/employee.txt
> without any success. Below is the error I am getting:
>
> > read.delim('https://online.stat.psu.edu/onlinecourses/sites/stat501/files/ch15/employee.txt')
>
> Error in make.names(col.names, unique = TRUE) :
>
>   invalid multibyte string at 't'
>
> In addition: Warning messages:
>
> 1: In read.table(file = file, header = header, sep = sep, quote = quote,  :
>
>   line 1 appears to contain embedded nulls
>
> 2: In read.table(file = file, header = header, sep = sep, quote = quote,  :
>
>   line 2 appears to contain embedded nulls
>
> 3: In read.table(file = file, header = header, sep = sep, quote = quote,  :
>
>   line 3 appears to contain embedded nulls
>
> 4: In read.table(file = file, header = header, sep = sep, quote = quote,  :
>
>   line 4 appears to contain embedded nulls
>
> 5: In read.table(file = file, header = header, sep = sep, quote = quote,  :
>
>   line 5 appears to contain embedded nulls
>
> Is there any way to read this data directly onto R?
>
> Thanks for your time
>
> __
> 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 https://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 https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] kinship package: drawing pedigree error

2008-02-06 Thread Iris Kolder
Hi

Im using the kinship package to draw a pedigree. On my data set this works fine 
but when i add indivudals to the pedigree i keep getting an error i hope 
someone can help me!

This is the code im using:

Data<-read.table("Tree.txt", header=T, sep=",")
attach(Data)
ped<-pedigree(id, dadid, momid, sex, aff)
par(xpd=T)
plot.pedigree(ped)

This is my data looks like without the last 3 individuals it works perfect,when 
i add them i get the following error:
Error in if (min(pos2) < 0) { : missing value where TRUE/FALSE needed

famid,id,dadid,momid,sex,aff
1,8860,9972,8856,2,0
1,8855,9972,8856,2,0
1,8859,9976,8860,2,0
1,8854,9971,8855,2,0
1,8863,9975,8859,2,0
1,8858,9975,8859,2,0
1,8865,9975,8859,2,0
1,9969,9970,8854,1,0
1,8864,9980,8863,2,0
1,8862,9980,8863,2,0
1,23834,9981,8865,2,0
1,9968,9969,8853,1,0
1,21141,9974,8858,1,0
1,21142,9974,8858,2,0
1,23172,265,21142,2,0
1,25409,265,21142,1,0
1,23171,265,21142,2,0
1,265,NA,NA,1,0
1,9981,NA,NA,1,0
1,8853,NA,NA,2,0
1,9974,NA,NA,1,0
1,9980,NA,NA,1,0
1,9972,NA,NA,1,0
1,8856,NA,NA,2,0
1,9976,NA,NA,1,0
1,9971,NA,NA,1,0
1,9975,NA,NA,1,0
1,9970,NA,NA,1,0

1,9979,NA,NA,1,0
1,20644,9979,8862,2,0
1,20670,9979,8862,1,0

I tried putting nuclear families closer together in de pedigree but this 
doesn't seem to help. All individuals have both parents in the pedigree or have 
2 missing parents.


I hope someone can help me with this!

Iris


  

Never miss a thing.  Make Yahoo your home page. 

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] kinship package: drawing pedigree error

2008-02-07 Thread Iris Kolder
I tried replacing the missing parents with 0 but got the same error 

Error in if (min(pos2) < 0) { : missing value where TRUE/FALSE needed

Any other thoughts?

Iris 

Subject: Re: [R] kinship package: drawing pedigree error

On Wed, 06-Feb-2008 at 04:49AM -0800, Iris Kolder wrote:

[...]

|> 1,9979,NA,NA,1,0
|> 1,20644,9979,8862,2,0
|> 1,20670,9979,8862,1,0
|> 

|> I tried putting nuclear families closer together in de pedigree but
|> this doesn't seem to help. All individuals have both parents in the
|> pedigree or have 2 missing parents.

It's a while since I used that function, but IIRC, a missing parent is
set as 0, not NA.  

It might have changed since I used it, in which case ignore me.

HTH


-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.  
  ___Patrick Connolly  
{~._.~}  Great minds discuss ideas
_( Y )_Middle minds discuss events 
(:_~*~_:)   Small minds discuss people  
(_)-(_). Anon
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.


  

Be a better friend, newshound, and 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] Matching and merging two rows with missing values

2007-10-11 Thread Iris Kolder
Hello, 

 

I have two rows which are almost identical but miss different values at
different locations. I would like to merge these two rows so that the
missing values are replaced by the element in the same column on the other
row making one row. If both rows contain a NA, NA remains in the output.

 

   1 2 3 4 5

Row1  AA AG GG NA NA

Row2  NA AG GG AA NA

 

The output i want is one row

 

1 2 3 4 5

Row AA AG GG AA NA

 

I tried to use the functions match,grep, and gsub but I can't seem to make
it work I would appreciate any suggestions!

 

Best regards,

 

Iris Kolder


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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] Matching and merging two rows with missing values

2007-10-11 Thread Iris Kolder
Thank you for your help this works!!

x <-c("AA","AG","GG",NA, NA)
y <- c(NA, "AG", "GG", "AA", NA)

x[is.na(x)] <- y[is.na(x)]


Hello, 

 

I have two rows which are almost identical but miss different values at
different locations. I would like to merge these two rows so that the
missing values are replaced by the element in the same column on the
other row making one row. If both rows contain a NA, NA remains in the
output.

 

   1 2 3 4 5

Row1  AA AG GG NA NA

Row2  NA AG GG AA NA

 

The output i want is one row

 

1 2 3 4 5

Row AA AG GG AA NA

 

I tried to use the functions match,grep, and gsub but I can't seem to
make it work I would appreciate any suggestions!

 

Best regards,

 

Iris Kolder


[[alternative HTML version deleted]]

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


This is not an offer (or solicitation of an offer) to buy/sell the
securities/instruments mentioned or an official confirmation.  Morgan
Stanley may deal as principal in or own or act as market maker for
securities/instruments mentioned or may advise the issuers.  This is not
research and is not from MS Research but it may refer to a research
analyst/research report.  Unless indicated, these views are the author's and
may differ from those of Morgan Stanley research or others in the Firm.  We
do not represent this is accurate or complete and we may not update this.
Past performance is not indicative of future returns.  For additional
information, research reports and important disclosures, contact me or see
https://secure.ms.com/servlet/cls.  You should not use e-mail to request,
authorize or effect the purchase or sale of any security or instrument, to
send transfer instructions, or to effect any other transactions.  We cannot
guarantee that any such requests received via e-mail will be processed in a
timely manner.  This communication is solely for the addressee(s) and may
contain confidential information.  We do not waive confidentiality by
mistransmission.  Contact me if you do not wish to receive these
communications.  In the UK, this communication is directed in the UK to
those persons who are market counterparties or intermediate customers (as
defined in the UK Financial Services Authority's rules).

__
R-help@r-project.org mailing list
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] readLines() and unz() and non-empty final line

2024-10-24 Thread Iris Simmons
Hi Mikko,


I tried running a few different things, and it seems as though
explicitly using `open()` and opening a blocking connection works.

```R
cat("hello", file = "hello.txt")
zip("hello.zip", "hello.txt")
local({
conn <- unz("hello.zip", "hello.txt")
on.exit(close(conn))
## you can use "r" instead of "rt"
##
## 'blocking = TRUE' is the default, so remove if desired
open(conn, "rb", blocking = TRUE)
readLines(conn)
})
```

A blocking connection might be undesirable for you, in which case
someone else might have a better solution.

On Thu, Oct 24, 2024 at 10:58 AM Marttila Mikko via R-help
 wrote:
>
> Dear list,
>
> I'm seeing a strange interaction with readLines() and unz() when reading
> a file without an empty final line. The final line gets dropped silently:
>
> > cat("hello", file = "hello.txt")
> > zip("hello.zip", "hello.txt")
>   adding: hello.txt (stored 0%)
> > readLines(unz("hello.zip", "hello.txt"))
> character(0)
>
> The documentation for readLines() says if the final line is incomplete for
> "non-blocking text-mode connections" the line is "pushed back, silently"
> but otherwise "accepted with a warning".
>
> My understanding is that the unz() here is blocking so the line should be
> accepted. Is that incorrect? If so, how would I go about reading such
> lines from a zip file?
>
> Best,
>
> Mikko
>
>
> This e-mail transmission may contain confidential or legally privileged 
> information that is intended only for the individual or entity named in the 
> e-mail address. If you are not the intended recipient, you are hereby 
> notified that any disclosure, copying, distribution, or reliance upon the 
> contents of this e-mail is strictly prohibited. If you have received this 
> e-mail transmission in error, please reply to the sender, so that they can 
> arrange for proper delivery, and then please delete the message from your 
> computer systems. Thank you.
>
> __
> 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 https://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 https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] readLines() and unz() and non-empty final line

2024-10-25 Thread Iris Simmons
Hi again,


The unz connection is non-blocking by default. I checked do_unz which calls
R_newunz which calls init_con and the only place in any of those functions
that sets 'blocking' is init_con which sets it to FALSE:

https://github.com/wch/r-source/blob/0c26529e807a9b1dd65f7324958c17bf72e1de1a/src/main/connections.c#L713

I'll open an issue on R-bugzilla and see if they're willing to do something
similar to 'file()'; that is, add a 'blocking' argument to unz. It's hard
to say whether they would choose 'blocking = FALSE' for back compatibility
or 'blocking = TRUE' for consistency with 'file()'.


Regards,

Iris

On Fri, Oct 25, 2024, 04:47 Marttila Mikko 
wrote:

> Thanks Iris, Bert, and Tim.
>
>
>
> Whether unz() is blocking or not by default doesn’t seem to be documented.
> Indeed, thank you Iris for finding out that explicitly opening it as
> blocking would work. That made me wonder if it’s non-blocking by default
> then, which would have been surprising. However, explicitly opening it as
> non-blocking seems to lead to problems as well:
>
>
>
> > local({
>
> +   con <- unz("hello.zip", "hello.txt")
>
> +   open(con, blocking = FALSE)
>
> +   on.exit(close(con))
>
> +   res <- readLines(con)
>
> +   res
>
> + })
>
> Error in readLines(con) : seek not enabled for this connection
>
> Calls: local ... eval.parent -> eval -> eval -> eval -> eval -> readLines
>
> Execution halted
>
> So, the behaviour of unz() seems to be different depending on whether it
> was explicitly opened before passed to readLines(). Should this be fixed or
> documented?
>
>
>
> Best,
>
>
>
> Mikko
>
>
>
> *From:* Bert Gunter 
> *Sent:* Thursday, 24 October 2024 18:13
> *To:* Iris Simmons 
> *Cc:* Marttila Mikko ;
> r-help@r-project.org
> *Subject:* Re: [R] readLines() and unz() and non-empty final line
>
>
>
> You don't often get email from bgunter.4...@gmail.com. Learn why this is
> important <https://aka.ms/LearnAboutSenderIdentification>
>
> But note:
>
>
>
> > zip("hello.zip", "hello.txt")
> updating: hello.txt (stored 0%)
> > readChar(unz("hello.zip","hello.txt"),100)
> [1] "hello"
>
>
>
> I leave it to you and other wiser heads to figure out.
>
>
>
> Cheers,
>
> Bert
>
>
>
> On Thu, Oct 24, 2024 at 8:57 AM Iris Simmons  wrote:
>
> Hi Mikko,
>
>
> I tried running a few different things, and it seems as though
> explicitly using `open()` and opening a blocking connection works.
>
> ```R
> cat("hello", file = "hello.txt")
> zip("hello.zip", "hello.txt")
> local({
> conn <- unz("hello.zip", "hello.txt")
> on.exit(close(conn))
> ## you can use "r" instead of "rt"
> ##
> ## 'blocking = TRUE' is the default, so remove if desired
> open(conn, "rb", blocking = TRUE)
> readLines(conn)
> })
> ```
>
> A blocking connection might be undesirable for you, in which case
> someone else might have a better solution.
>
> On Thu, Oct 24, 2024 at 10:58 AM Marttila Mikko via R-help
>  wrote:
> >
> > Dear list,
> >
> > I'm seeing a strange interaction with readLines() and unz() when reading
> > a file without an empty final line. The final line gets dropped silently:
> >
> > > cat("hello", file = "hello.txt")
> > > zip("hello.zip", "hello.txt")
> >   adding: hello.txt (stored 0%)
> > > readLines(unz("hello.zip", "hello.txt"))
> > character(0)
> >
> > The documentation for readLines() says if the final line is incomplete
> for
> > "non-blocking text-mode connections" the line is "pushed back, silently"
> > but otherwise "accepted with a warning".
> >
> > My understanding is that the unz() here is blocking so the line should be
> > accepted. Is that incorrect? If so, how would I go about reading such
> > lines from a zip file?
> >
> > Best,
> >
> > Mikko
> >
> >
> > This e-mail transmission may contain confidential or legally privileged
> information that is intended only for the individual or entity named in the
> e-mail address. If you are not the intended recipient, you are hereby
> notified that any disclosure, copying, distribution, or reliance upon the
> contents of this e-mail is strictly prohibited. If you have received this
> e-mail tran

Re: [R] outer() is not working with my simple function

2024-12-10 Thread Iris Simmons
outer() expects a return value with the same length as that of x.

You could change the code to rep(3, length(x))

Or if you don't want to change the code of the function, you could make a
wrapper and use that instead:
function (x, y)
{
v <- FN1(x, y)
if (length(v) != 1) v else rep(v, length(x))
}


On Tue, Dec 10, 2024, 11:37 Christofer Bogaso 
wrote:

> Hi,
>
> I have below code
>
> FN1 = function(x, y) 3
>
> outer(1:9, 1:9, FN1)
>
> With above I get error as below
>
> Error in dim(robj) <- c(dX, dY) :
>
>   dims [product 81] do not match the length of object [1]
>
> Could you please help to understand why is it failing? I am just
> expecting to get a matrix with all elements as 3
>
> __
> 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
> https://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 https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Parser For Line Number Tracing

2025-01-18 Thread Iris Simmons
Hi Ivo,


I maintain 'package:this.path' that I believe does what you want. I
regularly add this to my own code when I need to:

warning(sprintf("remove this later at %s#%d",
this.path::try.this.path(), this.path::LINENO()), call. = FALSE,
immediate. = TRUE)

Of course, modify as needed.


Regards,
Iris

On Sat, Jan 18, 2025 at 6:41 PM Ivo Welch  wrote:
>
> I often find myself hunting where in my program an error has happened,
> (of course, in R, many error messages are mysterious in themselves,
> too, making it even harder.)  the way I do it is mostly with inserting
> `message()` statements.
>
> what I would really like to have is a parser that inserted 'curline
> <<- ##' into the R code, where '##' is the filename and line number.
> something like 'addtracker one.R two.R' and thereafter I can run two.R
> and, when the program dies, use `print curline` to find out where my
> error has roughly occurred.
>
> has someone already written such an 'instrumenter'?
>
> __
> 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 https://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 https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Extracting specific arguments from "..."

2025-01-05 Thread Iris Simmons
I would use two because it does not force the evaluation of the other
arguments in the ... list.



On Sun, Jan 5, 2025, 13:00 Bert Gunter  wrote:

> Consider:
>
> f1 <- function(...){
>   one <- list(...)[['a']]
>   two <- ...elt(match('a', ...names()))
>   c(one, two)
> }
> ## Here "..." is an argument list with "a" somewhere in it, but in an
> unknown position.
>
> > f1(b=5, a = 2, c=7)
> [1] 2 2
>
> Which is better for extracting a specific named argument, one<- or
> two<- ?  Or a third alternative that is better than both?
> Comments and critiques welcome.
>
> Cheers,
> Bert
>
> __
> 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
> https://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 https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] What don't I understand about sample()?

2025-03-13 Thread Iris Simmons
The textbook uses an extra argument 'size'. If you do the same, it should
work.

matrix(sample(1:10, size = 5 * 10, replace=TRUE), 5, 10, byrow=TRUE)

On Thu, Mar 13, 2025, 17:23 Kevin Zembower via R-help 
wrote:

> Hello, all,
>
> I'm learning to do randomized distributions in my Stats 101 class*. I
> thought I could do it with a call to sample() inside a matrix(), like:
>
> > matrix(sample(1:10, replace=TRUE), 5, 10, byrow=TRUE)
>  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]823182889 8
> [2,]823182889 8
> [3,]823182889 8
> [4,]823182889 8
> [5,]823182889 8
> >
>
> Imagine my surprise to learn that all the rows were the same
> permutation. I thought each time sample() was called inside the matrix,
> it would generate a different permutation.
>
> I modeled this after the bootstrap sample techniques in
> https://pages.stat.wisc.edu/~larget/stat302/chap3.pdf. I don't
> understand why it works in bootstrap samples (with replace=TRUE), but
> not in randomized distributions (with replace=FALSE).
>
> Thanks for any insight you can share with me, and any suggestions for
> getting rows in a matrix with different permutations.
>
> -Kevin
>
> *No, this isn't a homework problem. We're using Lock5 as the text in
> class, along with its StatKey web application. I'm just trying to get
> more out of the class by also solving our problems using R, for which
> I'm not receiving any class credit.
>
> __
> 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
> https://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 https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.