Re: [R] can't install nser...

2023-04-09 Thread Andrew Simmons
It says that nser requires the most recent version of magrittr that you do
not have installed. You must update magrittr before attempting to install
nser:

update.packages(oldPkgs = "magrittr")

or at the prompt you were presented before, choose to update magrittr
before installing nser.

On Sun, Apr 9, 2023, 17:55 akshay kulkarni  wrote:

> Dear members,
>  I can't install "nser" package. It is not in
> cran but install_version and install_github both are not working:
>
> > install_version("nser",version = "1.4.0")
> Downloading package from url:
> https://cran.rstudio.com//src/contrib/Archive/nser/nser_1.4.0.tar.gz
> These packages have more recent versions available.
> It is recommended to update all of them.
> Which would you like to update?
>
>  1: All
>  2: CRAN packages only
>  3: None
>  4: rlang(1.0.4 -> 1.1.0 ) [CRAN]
>  5: cli  (3.3.0 -> 3.6.1 ) [CRAN]
>  6: openssl  (1.4.5 -> 2.0.6 ) [CRAN]
>  7: curl (4.3.2 -> 5.0.0 ) [CRAN]
>  8: Rcpp (1.0.7 -> 1.0.10) [CRAN]
>  9: ps   (1.6.0 -> 1.7.4 ) [CRAN]
> 10: processx (3.5.2 -> 3.8.0 ) [CRAN]
> 11: purrr(0.3.4 -> 1.0.1 ) [CRAN]
> 12: magrittr (2.0.1 -> 2.0.3 ) [CRAN]
> 13: fastmap  (1.1.0 -> 1.1.1 ) [CRAN]
> 14: cachem   (1.0.6 -> 1.0.7 ) [CRAN]
> 15: fs   (1.5.2 -> 1.6.1 ) [CRAN]
>
> Enter one or more numbers, or an empty line to skip updates:
> * installing *source* package 'nser' ...
> ** package 'nser' successfully unpacked and MD5 sums checked
> ** using staged installation
> ** R
> ** data
> *** moving datasets to lazyload DB
> ** inst
> ** byte-compile and prepare package for lazy loading
> Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck
> = vI[[j]]) :
>   namespace 'magrittr' 2.0.1 is being loaded, but >= 2.0.3 is required
> Calls:  ... namespaceImportFrom -> asNamespace -> loadNamespace
> Execution halted
> ERROR: lazy loading failed for package 'nser'
> * removing 'C:/Program Files/R/R-4.1.2/library/nser'
> Warning message:
> In i.p(...) :
>   installation of package
> ‘C:/Users/ADMINI~1/AppData/Local/Temp/2/RtmpktmxP2/remotesbec536a2c/nser’
> had non-zero exit status
>
>
> Can you please help?
>
> Thanking you,
> Yours sincerely,
> AKSHAY M MKULKARNI
>
> [[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] Removing variables from data frame with a wile card

2023-02-12 Thread Andrew Simmons
What I meant is that that

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

and

mydata[!grepl("^yr", colnames(mydata))]

should be identical. Some people would prefer the first because the
indexing looks the same as matrix indexing, whereas some people would
prefer the second because it is more efficient. However, I would argue
it is exactly as efficient. You can see from the first few lines of
`[.data.frame` when the first index is missing and the second is
provided, it does almost the same thing as if only the first index
provided.

On Sun, Feb 12, 2023 at 9:38 PM Steven Yen  wrote:
>
> x[“V2”] would retain columns of x headed by V2. What I need is the 
> opposite——I need a data grime with those columns excluded.
>
> Steven from iPhone
>
> On Feb 13, 2023, at 9:33 AM, Rolf Turner  wrote:
>
> 
> On Sun, 12 Feb 2023 14:57:36 -0800
> Jeff Newmiller  wrote:
>
> x["V2"]
>
>
> is more efficient than using drop=FALSE, and perfectly normal syntax
>
> (data frames are lists of columns).
>
>
> 
>
> I never cease to be amazed by the sagacity and perspicacity of the
> designers of R.  I  would have worried that x["V2"] would turn out to be
> a *list* (of length 1), but no, it retains the data.frame class, which
> is clearly the Right Thing To Do.
>
> cheers,
>
> Rolf
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Stats. Dep't. phone: +64-9-373-7599 ext. 89622
> Home phone: +64-9-480-4619
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2023-02-12 Thread Andrew Simmons
drop = FALSE means that should the indexing select exactly one column, then
return a data frame with one column, instead of the object in the column.
It's usually not necessary, but I've messed up some data before by assuming
the indexing always returns a data frame when it doesn't, so drop = FALSE
let's me that I will always get a data frame.

```
x <- data.frame(V1 = 1:5, V2 = letters[1:5])
x[, "V2"]
x[, "V2", drop = FALSE]
```

You'll notice that the first returns a character vector, a through e, where
the second returns a data frame with one column where the object in the
column is the same character vector.

You could alternatively use

x["V2"]

which should be identical to x[, "V2", drop = FALSE], but some people don't
like that because it doesn't look like matrix indexing anymore.


On Sun, Feb 12, 2023, 17:18 Steven T. Yen  wrote:

> In the line suggested by Andrew Simmons,
>
> mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE]
>
> what does drop=FALSE do? Thanks.
>
> On 1/14/2023 8:48 PM, Steven Yen wrote:
>
> Thanks to all. Very helpful.
>
> Steven from iPhone
>
> On Jan 14, 2023, at 3:08 PM, Andrew Simmons 
>  wrote:
>
> You'll want to use grep() or grepl(). By default, grep() uses extended
> regular expressions to find matches, but you can also use perl regular
> expressions and globbing (after converting to a regular expression).
> For example:
>
> grepl("^yr", colnames(mydata))
>
> will tell you which 'colnames' start with "yr". If you'd rather you
> use globbing:
>
> grepl(glob2rx("yr*"), colnames(mydata))
>
> Then you might write something like this to remove the columns starting
> with yr:
>
> mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE]
>
> On Sat, Jan 14, 2023 at 1:56 AM Steven T. Yen 
>  wrote:
>
>
> I have a data frame containing variables "yr3",...,"yr28".
>
>
> How do I remove them with a wild cardsomething similar to "del yr*"
>
> in Windows/doc? Thank you.
>
>
> colnames(mydata)
>
>   [1] "year"   "weight" "confeduc"   "confothr" "college"
>
>   [6] ...
>
>  [41] "yr3""yr4""yr5""yr6" "yr7"
>
>  [46] "yr8""yr9""yr10"   "yr11" "yr12"
>
>  [51] "yr13"   "yr14"   "yr15"   "yr16" "yr17"
>
>  [56] "yr18"   "yr19"   "yr20"   "yr21" "yr22"
>
>  [61] "yr23"   "yr24"   "yr25"   "yr26" "yr27"
>
>  [66] "yr28"...
>
>
> __
>
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>
> https://stat.ethz.ch/mailman/listinfo/r-help
>
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>
> and provide commented, minimal, self-contained, reproducible code.
>
>

[[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] preserve class in apply function

2023-02-07 Thread Andrew Simmons
It is not possible, apply() converts its argument to an array. You might be
able to use split() and lapply() to solve your problem.

On Tue, Feb 7, 2023, 07:52 Naresh Gurbuxani 
wrote:

>
> > Consider a data.frame whose different columns have numeric, character,
> > and factor data.  In apply function, R seems to pass all elements of a
> > row as character.  Is it possible to preserve numeric class?
> >
> >> mydf <- data.frame(x = rnorm(10), y = runif(10))
> >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> 0.31351909
> > [7] -0.63575990  0.22670658  0.55696314  0.39587314
> >> mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
> >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary
> operator
> >> apply(mydf, 1, function(row) {as.numeric(row["x"]) +
> as.numeric(row["y"])})
> > [1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338
> 0.31351912
> > [7] -0.63575991  0.22670663  0.55696309  0.39587311
> >> apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})
> > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> 0.31351909
> > [7] -0.63575990  0.22670658  0.55696314  0.39587314
>
> __
> 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] implicit loop for nested list

2023-01-26 Thread Andrew Simmons
I would use replicate() to do an operation with random numbers repeatedly:

```
mysim <- replicate(10, {
two.mat <- matrix(rnorm(4), 2, 2)
four.mat <- matrix(rnorm(16), 4, 4)
list(two.mat = two.mat, four.mat = four.mat)
})
```

which should give you a matrix-list. You can slice this matrix-list
just like normal, then cbind it in one step:

```
two.mat <- do.call("cbind", mysim["two.mat", ])
four.mat <- do.call("cbind", mysim["four.mat", ])
```

On Thu, Jan 26, 2023 at 10:33 PM Naresh Gurbuxani
 wrote:
>
> >
> > I am looking for a more elegant way to write below code.
> >
> > #Simulation results have different dimensions
> > mysim <- lapply(1:10, function(y) {
> >two.mat <- matrix(rnorm(4), nrow = 2)
> >four.mat <- matrix(rnorm(16), nrow = 4)
> >list(two.mat = two.mat, four.mat = four.mat) #results with different 
> > dimensions
> > })
> >
> > #Collect different components of simulation results
> > #Is it possible to do this with implicit loops?
> > mat2 <- matrix(nrow = 2, ncol = 1)
> > mat4 <- matrix(nrow = 4, ncol = 1)
> > for (mat.list in mysim) {
> >mat2 <- cbind(mat2, mat.list[["two.mat"]])
> >mat4 <- cbind(mat4, mat.list[["four.mat"]])
> > }
> > mat2 <- mat2[,-1]
> > mat4 <- mat4[,-1]
>
> __
> 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] Minimal match to regexp?

2023-01-25 Thread Andrew Simmons
It seems like a bug to me. Using perl = TRUE, I see the desired result:

```
x <- "\n```html\nblah blah \n```\n\n```r\nblah blah\n```\n"

pattern2 <- "\n([`]{3,})html\n.*?\n\\1\n"

cat(regmatches(x, regexpr(pattern2, x, perl = TRUE)))
```

If you change it to something like:

```
x <- c(
"\n```html\nblah blah \n```\n\n```r\nblah blah\n```\n",
"\n```html\nblah blah \n```\n"
)

pattern2 <- "\n([`]{3,})html\n.*?\n\\1\n"

print(regmatches(x, regexpr(pattern2, x)), width = 10)
```

you can see that it does find the match, so the combination of *? and
\\1 must be messing up regexpr(). They seem to work perfectly fine on
their own.

On Wed, Jan 25, 2023 at 7:57 PM Duncan Murdoch  wrote:
>
> Thanks for pointing out my mistake.  I oversimplified the real problem.
>
> I'll try to post a version of it that comes closer:  Suppose I have a
> string like this:
>
> x <- "\n```html\nblah blah \n```\n\n```r\nblah blah\n```\n"
>
> If I cat() it, I see that it is really markdown source:
>
>```html
>blah blah
>```
>
>```r
>blah blah
>```
>
> I want to find the part that includes the html block, but not the r
> block.  So I want to match "```html", followed by a minimal number of
> characters, then "```".  Then this pattern works:
>
>pattern <- "\n```html\n.*?\n```\n"
>
> and we get the right answer:
>
>cat(regmatches(x, regexpr(pattern, x)))
>
>```html
>blah blah
>```
>
> Okay, but this flavour of markdown says there can be more backticks, not
> just 3.  So the block might look like
>
>html
>blah blah
>
>
> I need to have the same number of backticks in the opening and closing
> marker.  So I make the pattern more complicated, and it doesn't work:
>
>pattern2 <- "\n([`]{3,})html\n.*?\n\\1\n"
>
> This matches all of x:
>
>> pattern2 <- "\n([`]{3,})html\n.*?\n\\1\n"
>> cat(regmatches(x, regexpr(pattern2, x)))
>
>```html
>blah blah
>```
>
>```r
>blah blah
>```
>
>
> Is that a bug, or am I making a silly mistake again?
>
> Duncan Murdoch
>
>
>
> On 25/01/2023 7:34 p.m., Andrew Simmons wrote:
> > grep(value = TRUE) just returns the strings which match the pattern. You
> > have to use regexpr() or gregexpr() if you want to know where the
> > matches are:
> >
> > ```
> > x <- "abaca"
> >
> > # extract only the first match with regexpr()
> > m <- regexpr("a.*?a", x)
> > regmatches(x, m)
> >
> > # or
> >
> > # extract every match with gregexpr()
> > m <- gregexpr("a.*?a", x)
> > regmatches(x, m)
> > ```
> >
> > You could also use sub() to remove the rest of the string:
> > `sub("^.*(a.*?a).*$", "\\1", x)`
> > keeping only the match within the parenthesis.
> >
> >
> > On Wed, Jan 25, 2023, 19:19 Duncan Murdoch  > <mailto:murdoch.dun...@gmail.com>> wrote:
> >
> > The docs for ?regexp say this:  "By default repetition is greedy, so
> > the
> > maximal possible number of repeats is used. This can be changed to
> > ‘minimal’ by appending ? to the quantifier. (There are further
> > quantifiers that allow approximate matching: see the TRE
> > documentation.)"
> >
> > I want the minimal match, but I don't seem to be getting it.  For
> > example,
> >
> > x <- "abaca"
> > grep("a.*?a", x, value = TRUE)
> > #> [1] "abaca"
> >
> > Shouldn't I have gotten "aba", which is the first match to "a.*a"?  If
> > not, what would be the regexp that would give me the first match to
> > "a.*a", without greedy expansion of the .*?
> >
> > Duncan Murdoch
> >
> > __
> > R-help@r-project.org <mailto:R-help@r-project.org> mailing list --
> > To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > <https://stat.ethz.ch/mailman/listinfo/r-help>
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > <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] Minimal match to regexp?

2023-01-25 Thread Andrew Simmons
grep(value = TRUE) just returns the strings which match the pattern. You
have to use regexpr() or gregexpr() if you want to know where the matches
are:

```
x <- "abaca"

# extract only the first match with regexpr()
m <- regexpr("a.*?a", x)
regmatches(x, m)

# or

# extract every match with gregexpr()
m <- gregexpr("a.*?a", x)
regmatches(x, m)
```

You could also use sub() to remove the rest of the string:
`sub("^.*(a.*?a).*$", "\\1", x)`
keeping only the match within the parenthesis.


On Wed, Jan 25, 2023, 19:19 Duncan Murdoch  wrote:

> The docs for ?regexp say this:  "By default repetition is greedy, so the
> maximal possible number of repeats is used. This can be changed to
> ‘minimal’ by appending ? to the quantifier. (There are further
> quantifiers that allow approximate matching: see the TRE documentation.)"
>
> I want the minimal match, but I don't seem to be getting it.  For example,
>
> x <- "abaca"
> grep("a.*?a", x, value = TRUE)
> #> [1] "abaca"
>
> Shouldn't I have gotten "aba", which is the first match to "a.*a"?  If
> not, what would be the regexp that would give me the first match to
> "a.*a", without greedy expansion of the .*?
>
> 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] as.factor and floating point numbers

2023-01-25 Thread Andrew Simmons
R converts floats to strings with ~15 digits of accuracy, specifically
to avoid differentiating between 1 and 1 + .Machine$double.eps, it is
assumed that small differences such as this are due to rounding errors
and are unimportant.

So, if when making your factor, you want all digits, you could write
this: `as.factor(format(x, digits = 17L))`

On Wed, Jan 25, 2023 at 4:03 AM Tobias Fellinger  wrote:
>
> Hello,
>
> I'm encountering the following error:
>
> In a package for survival analysis I use a data.frame is created, one column 
> is created by applying unique on the event times while others are created by 
> running table on the event times and the treatment arm.
>
> When there are event times very close together they are put in the same 
> factor level when coerced to factor while unique outputs both values, leading 
> to different lengths of the columns.
>
> Try this to reproduce:
> x <- c(1, 1+.Machine$double.eps)
> unique(x)
> table(x)
>
> Is there a general best practice to deal with such issues?
>
> Should calling table on floats be avoided in general?
>
> What can one use instead?
>
> One could easily iterate over the unique values and compare all values with 
> the whole vector but this are N*N comparisons, compared to N*log(N) when 
> sorting first and taking into account that the vector is sorted.
>
> I think for my purposes I'll round to a hundredth of a day before calling the 
> function, but any advice on avoiding this issue an writing more fault 
> tolerant code is greatly appreciated.
>
> all the best, Tobias
>
>
> [[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] Partial matching with $ extractor.

2023-01-24 Thread Andrew Simmons
I tried this again with R 2.15.3, the oldest version I have installed,
and I still got the same behaviour. It extracts the first exact match,
then the only partial match, then NULL.

On Tue, Jan 24, 2023 at 5:04 PM Rolf Turner  wrote:
>
>
> Has something changed, but I missed it?
>
> My recollection is that $ extraction used partial matching.
>
> E.g. if one did
>
> junk <- list(yuck=1,yurk=2,y=3)
> junk$y
>
> then one would get 1 as the result; probably *not* the desired result.
> See fortunes::fortune("toad").
>
> To get the desired result, one would need to use junk[["y"]].
>
> Likewise junk$yu would give 1; to get the value of "yurk", one would
> need to use junk$yur or junk$yurk or (better?) junk[["yurk"]].
>
> However, either my recollection is wrong, or something has changed.
> When I do junk$y I get 3 (the "right" answer; the same as junk[["y"]]).
> When I do junk$yu I get NULL (just as if I'd done junk[["yu"]]).
>
> So: has something changed, or am I miss-remembering, or am I completely
> confused about the whole issue?
>
> Thanks for any enlightenment.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-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] Partial matching with $ extractor.

2023-01-24 Thread Andrew Simmons
junk$y extracts the element named "y" because it found an exact match for
the name.

junk$yu extracts nothing because it does not find an exact match and finds
multiple partial matches.

junk$yuc or junk$yur would work because it finds no exact match and then
exactly one partial match.

On Tue, Jan 24, 2023, 17:04 Rolf Turner  wrote:

>
> Has something changed, but I missed it?
>
> My recollection is that $ extraction used partial matching.
>
> E.g. if one did
>
> junk <- list(yuck=1,yurk=2,y=3)
> junk$y
>
> then one would get 1 as the result; probably *not* the desired result.
> See fortunes::fortune("toad").
>
> To get the desired result, one would need to use junk[["y"]].
>
> Likewise junk$yu would give 1; to get the value of "yurk", one would
> need to use junk$yur or junk$yurk or (better?) junk[["yurk"]].
>
> However, either my recollection is wrong, or something has changed.
> When I do junk$y I get 3 (the "right" answer; the same as junk[["y"]]).
> When I do junk$yu I get NULL (just as if I'd done junk[["yu"]]).
>
> So: has something changed, or am I miss-remembering, or am I completely
> confused about the whole issue?
>
> Thanks for any enlightenment.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

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

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

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

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

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

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

On Sat, Jan 14, 2023 at 1:56 AM Steven T. Yen  wrote:
>
> I have a data frame containing variables "yr3",...,"yr28".
>
> How do I remove them with a wild cardsomething similar to "del yr*"
> in Windows/doc? Thank you.
>
>  > colnames(mydata)
>[1] "year"   "weight" "confeduc"   "confothr" "college"
>[6] ...
>   [41] "yr3""yr4""yr5""yr6" "yr7"
>   [46] "yr8""yr9""yr10"   "yr11" "yr12"
>   [51] "yr13"   "yr14"   "yr15"   "yr16" "yr17"
>   [56] "yr18"   "yr19"   "yr20"   "yr21" "yr22"
>   [61] "yr23"   "yr24"   "yr25"   "yr26" "yr27"
>   [66] "yr28"...
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2023-01-09 Thread Andrew Simmons
Returning the last value of { is the basis of functions not needing a
return statement. Before R invokes a function (specifically a closure), it
creates a new context. When R evaluates a call to return, it looks for a
context to return from and finds the context of function, ending the
context and the evaluation of the function body early. However, if you
don't use return, R just returns the value from evaluating the function
body, and if your function body starts with {, it will return the last
expression from the function body, as desired.

On Mon, Jan 9, 2023, 12:15 akshay kulkarni  wrote:

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

[[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 integrate(function(x) x^3 / sin(x), -pi/2, pi/2)

2023-01-07 Thread Andrew Simmons
`subdivisions` is the maximum number of subintervals. Looking here

https://github.com/wch/r-source/blob/79298c499218846d14500255efd622b5021c10ec/src/appl/integrate.c#L1275

I'm not surprised that changing `subdivisions` has no effect on the
outcome. The integration method from {pracma} might work, maybe it
never calculates the function at 0, maybe it's using some alternate
method. Or maybe it did calculate f(0) and got NaN and just assumed
that the limit converged. Maybe that's a fair assumption, and maybe
it's not.

However:

integrate(function(x) ifelse(x == 0, 0, x^3/sin(x), -pi/2, pi/2)

works perfectly fine and is a better function definition anyway.
`integrate` in the {stats} package is unlikely to change, so use the
alternate function definition or use {pracma}.

On Sat, Jan 7, 2023 at 10:41 PM Leonard Mada  wrote:
>
> Dear Andrew,
>
>
> The limit when x approaches 0 is 0. I think most integration algorithms 
> handle this situation.
>
>
> This actually works, and it "includes" formally the point x = 0:
>
> integrate(function(x) x^3 / sin(x), -pi/2, pi/2 + 1E-10)
>
>
> Trying to "bypass" the 0 using subdivisions unfortunately did not work:
>
> integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4097) # or 4096
>
>
> Sincerely,
>
>
> Leonard
>
>
> On 1/8/2023 5:32 AM, Andrew Simmons wrote:
>
> You're dividing 0 by 0, giving you NaN, perhaps you should try
>
> function(x) ifelse(x == 0, 0, x^3/sin(x))
>
> On Sat, Jan 7, 2023, 22:24 Leonard Mada via R-help  
> wrote:
>>
>> Dear List-Members,
>>
>> I encounter a problem while trying to integrate the following function:
>>
>> integrate(function(x) x^3 / sin(x), -pi/2, pi/2)
>> # Error in integrate(function(x) x^3/sin(x), -pi/2, pi/2) :
>> #  non-finite function value
>>
>> # the value should be finite:
>> curve(x^3 / sin(x), -pi/2, pi/2)
>> integrate(function(x) x^3 / sin(x), -pi/2, 0)
>> integrate(function(x) x^3 / sin(x), 0, pi/2)
>> # but this does NOT work:
>> integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4096)
>> integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4097)
>> # works:
>> integrate(function(x) x^3 / sin(x), -pi/2, pi/2 + 1E-10)
>>
>>
>> # Note: works directly with other packages
>>
>> pracma::integral(function(x) x^3 / sin(x), -pi/2, pi/2 )
>> # 3.385985
>>
>>
>> I hope that integrate() gets improved in base R as well.
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>> __
>> 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] Problem with integrate(function(x) x^3 / sin(x), -pi/2, pi/2)

2023-01-07 Thread Andrew Simmons
You're dividing 0 by 0, giving you NaN, perhaps you should try

function(x) ifelse(x == 0, 0, x^3/sin(x))

On Sat, Jan 7, 2023, 22:24 Leonard Mada via R-help 
wrote:

> Dear List-Members,
>
> I encounter a problem while trying to integrate the following function:
>
> integrate(function(x) x^3 / sin(x), -pi/2, pi/2)
> # Error in integrate(function(x) x^3/sin(x), -pi/2, pi/2) :
> #  non-finite function value
>
> # the value should be finite:
> curve(x^3 / sin(x), -pi/2, pi/2)
> integrate(function(x) x^3 / sin(x), -pi/2, 0)
> integrate(function(x) x^3 / sin(x), 0, pi/2)
> # but this does NOT work:
> integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4096)
> integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4097)
> # works:
> integrate(function(x) x^3 / sin(x), -pi/2, pi/2 + 1E-10)
>
>
> # Note: works directly with other packages
>
> pracma::integral(function(x) x^3 / sin(x), -pi/2, pi/2 )
> # 3.385985
>
>
> I hope that integrate() gets improved in base R as well.
>
>
> Sincerely,
>
>
> Leonard
>
> __
> 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] Date order question

2023-01-04 Thread Andrew Simmons
I converted `date` to a factor and it seemed to work:


```
library(ggplot2)
library(cowplot)
date <- c("12-29","12-30","01-01")
date <- factor(date, labels = unique(date))
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()
```

On Wed, Jan 4, 2023 at 4:09 PM Thomas Subia
 wrote:
>
> Colleagues,
>
> date<-c("12-29","12-30","01-01")
> PT <- c(.106,.130,.121)
> data <- data.frame(date,PT)
> ggplot(data, aes(x=date,y=PT,group=1))+
>   geom_point(size=4)+
>   geom_line()+
>   geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
>   
> scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
>   annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
>   annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
>   annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
>   annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
>   annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
>   annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
>   theme_cowplot()
>
> The plot has the wrong date order.
> What is desired is 12-29, 12-30 and 01-01.
>
> Some feedback would be appreciated.
>
> All the best,
> Thomas Subia
>
> "De quoi devenir chevre? Des donnees"
>
> __
> 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] error in exists.....

2022-12-27 Thread Andrew Simmons
exists() is for bindings in an environment, not for names in a list. try

"T1A1" %in% names(E$L[[i]])

instead


On Tue, Dec 27, 2022, 12:36 akshay kulkarni  wrote:

> Dear members,
>  I have the following code:
> >  E <- new.env()
> >  E$L <- list()
> >  i <- 1
> >   E$L[[i]]$T1A1 <- Sys.time()
> > exists("T1A1", where = E$L[[i]])
> Error in list2env(list(1, T1A1 = 1672161002.38743), NULL, ) :
>   attempt to use zero-length variable name
>
> I want the output of the exists() function to be TRUE. In any case:
>
> > E$L[[1]]$T1A1
> [1] "2022-12-27 22:40:02 IST"
>
> Please help me solve this conundrum
>
> 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] difference between script and a function....

2022-12-24 Thread Andrew Simmons
1. The execution environment for a script is the global environment. Each R
script run from a shell will be given its own global environment. Each R
session has exactly one global environment, but you can have several active
R sessions.

2. Using return in a script instead of a function will throw an error

Error: no function to return from, jumping to top level

3. You can use print(), cat(), and writeLines() to write to the output. It
does not save your R objects before it exits the script. You could use
save() or save.image() to save your objects, or possibly saveRDS() if you
are only looking to save one object. You could also use source() if you
just want the objects from another script.

4. Will you have shared access to the objects in another R session? No,
objects are not shared, unless you've got something weird setup with
external pointers. Each session has it owns global environment.

5. Any of the doc pages for the functions I listed above would help, you
can also do

?utils::Rscript

[[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] interval between specific characters in a string...

2022-12-02 Thread Andrew Simmons
try

gregexpr('b+', target_string)

which looks for one or more b characters, then get the attribute
"match.length"

On Fri, Dec 2, 2022, 18:56 Evan Cooch  wrote:

> Was wondering if there is an 'efficient/elegant' way to do the following
> (without tidyverse). Take a string
>
> abaaabbabaaab
>
> Its easy enough to count the number of times the character 'b' shows up
> in the string, but...what I'm looking for is outputing the 'intervals'
> between occurrences of 'b' (starting the counter at the beginning of the
> string). So, for the preceding example, 'b' shows up in positions
>
> 2, 6, 7, 13, 17
>
> So, the interval data would be: 2, 4, 1, 6, 4
>
> My main approach has been to simply output positions (say, something
> like unlist(gregexpr('b', target_string))), and 'do the math' between
> successive positions. Can anyone suggest a more elegant approach?
>
> Thanks in advance...
>
> __
> 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] Format printing with R

2022-11-21 Thread Andrew Simmons
For print(), digits is the minimal number of significant digits. In
your case, rounding the first column to the 3rd decimal place gives at
least 2 sigfigs and rounding the second column to the 2nd decimal
place.

If you want to print all numbers to two significant digits, regardless
of what other numbers are in the column, use signif() before print():

Mean <- c(0.3107966, 0.1880302, 45.3185794, 0.506637, 0.493363,
0.702915, 0.297085, 0.7967066, 0.2032934, 0.6582301, 0.3417699,
0.7262913, 0.2737087, 0.6415484, 0.3584516, 0.9110264, 0.0889736,
0.5211453, 0.4788547, 0.5481055, 0.4518945, 0.913509, 0.086491,
0.8727269, 0.1272731, 0.1015717, 0.6043692, 0.2940592, 0.2735274,
0.3777426, 0.34873, 0.1603127, 0.1723783, 0.1230961, 0.1779381,
0.0964334, 0.1584698, 0.1113717, 0.3349813, 0.4081109, 0.2569078,
0.1034356, 0.6741233, 0.1254412, 0.096, 0.0587457, 0.4401115,
0.4689114, 0.0322313, 0.5907618, 0.1591195, 0.1132923, 0.1124207,
0.0244058, 0.7058787, 0.2941213, 0.0746892, 0.474911, 0.3471837,
0.0435036, 0.0597126, 0.0478775, 0.1152615, 0.2074968, 0.2440626,
0.1605995, 0.0804598, 0.1442422, 0.3443231, 0.428056, 0.0528221,
0.0805222, 0.0457169, 0.0485596, 0.1333443, 0.0932917, 0.0653987,
0.0573934, 0.1399086, 0.0887337, 0.0984479, 0.0914421, 0.1155505,
0.1363764, 0.113457, 1.2985286)
Std.dev <- c(0.46282, 0.390736, 16.313635, 0.499956, 0.499956,
0.456974, 0.456974, 0.402449, 0.402449, 0.474303, 0.474303, 0.445861,
0.445861, 0.479546, 0.479546, 0.284706, 0.284706, 0.499553, 0.499553,
0.49768, 0.49768, 0.281088, 0.281088, 0.333279, 0.333279, 0.302084,
0.488986, 0.455619, 0.445769, 0.484823, 0.476568, 0.366896, 0.377709,
0.328547, 0.382461, 0.295185, 0.365181, 0.314592, 0.471984, 0.491484,
0.436928, 0.304527, 0.468701, 0.331218, 0.295958, 0.235148, 0.4964,
0.499033, 0.176614, 0.491693, 0.365787, 0.31695, 0.315883, 0.154305,
0.455647, 0.455647, 0.262889, 0.49937, 0.476075, 0.203988, 0.236954,
0.213507, 0.319337, 0.405514, 0.42953, 0.367161, 0.272004, 0.351335,
0.475147, 0.494797, 0.223678, 0.2721, 0.20887, 0.214945, 0.339946,
0.290841, 0.247228, 0.232593, 0.346892, 0.284359, 0.297919, 0.288237,
0.319685, 0.343188, 0.317151, 0.739096)
print(signif(cbind(Mean, Std.dev), 2))

which looks like:

> print(signif(cbind(Mean, Std.dev), 2))
Mean Std.dev
 [1,]  0.3100.46
 [2,]  0.1900.39
 [3,] 45.000   16.00
 [4,]  0.5100.50
 [5,]  0.4900.50
 [6,]  0.7000.46
 [7,]  0.3000.46
 [8,]  0.8000.40
 [9,]  0.2000.40
[10,]  0.6600.47
[11,]  0.3400.47
[12,]  0.7300.45
[13,]  0.2700.45
[14,]  0.6400.48
[15,]  0.3600.48
[16,]  0.9100.28
[17,]  0.0890.28
[18,]  0.5200.50
[19,]  0.4800.50
[20,]  0.5500.50
[21,]  0.4500.50
[22,]  0.9100.28
[23,]  0.0860.28
[24,]  0.8700.33
[25,]  0.1300.33
[26,]  0.1000.30
[27,]  0.6000.49
[28,]  0.2900.46
[29,]  0.2700.45
[30,]  0.3800.48
[31,]  0.3500.48
[32,]  0.1600.37
[33,]  0.1700.38
[34,]  0.1200.33
[35,]  0.1800.38
[36,]  0.0960.30
[37,]  0.1600.37
[38,]  0.1100.31
[39,]  0.3300.47
[40,]  0.4100.49
[41,]  0.2600.44
[42,]  0.1000.30
[43,]  0.6700.47
[44,]  0.1300.33
[45,]  0.0970.30
[46,]  0.0590.24
[47,]  0.4400.50
[48,]  0.4700.50
[49,]  0.0320.18
[50,]  0.5900.49
[51,]  0.1600.37
[52,]  0.1100.32
[53,]  0.1100.32
[54,]  0.0240.15
[55,]  0.7100.46
[56,]  0.2900.46
[57,]  0.0750.26
[58,]  0.4700.50
[59,]  0.3500.48
[60,]  0.0440.20
[61,]  0.0600.24
[62,]  0.0480.21
[63,]  0.1200.32
[64,]  0.2100.41
[65,]  0.2400.43
[66,]  0.1600.37
[67,]  0.0800.27
[68,]  0.1400.35
[69,]  0.3400.48
[70,]  0.4300.49
[71,]  0.0530.22
[72,]  0.0810.27
[73,]  0.0460.21
[74,]  0.0490.21
[75,]  0.1300.34
[76,]  0.0930.29
[77,]  0.0650.25
[78,]  0.0570.23
[79,]  0.1400.35
[80,]  0.0890.28
[81,]  0.0980.30
[82,]  0.0910.29
[83,]  0.1200.32
[84,]  0.1400.34
[85,]  0.1100.32
[86,]  1.3000.74
>

R will still print 3 decimal places for the third column since it
wants them to be of the same format, but each number is 2 sigfigs.

On Mon, Nov 21, 2022 at 3:41 PM Steven T. Yen via R-help
 wrote:
>
> Hi, I have two variables with 86 observations each. Below I print with
> the print command with digit=2. But, I am getting three decimal places
> for my first variable and two for the second. Please help. Thanks.
>
>  > cbind(Mean,Std.dev)
>  Mean   Std.dev
>   [1,]  0.3107966  0.462820
>   [2,]  0.1880302  0.390736
>   [3,] 45.3185794 16.313635
>   [4,]  0.5066370  0.499956
>   [5,]  0.4933630  0.499956
>   [6,]  0.7029150  0.456974
>   [7,]  0.2970850  0.456974
>   [8,]  0.7967066  0.402449
>   [9,]  0.2032934  0.402449
> [10,]  0.6582301  0.474303
> [11,]  0.3417699  0.474303

Re: [R] Rare behaviour for nlme::reStruct example and question about ?lmeObject

2022-11-15 Thread Andrew Simmons
This seems to be a bug. I tried creating this function in the global
environment:

str.pdMat <- function (object, ...)
{
if (nlme::isInitialized(object)) {
NextMethod()
}
else {
cat(" Uninitialized positive definite matrix structure of class ",
class(object)[1], ".\n", sep = "")
}
}

and the code you sent works:

> library(nlme)
> rs1 <- reStruct(list(Dog = ~day, Side = ~1), data = Pixel)
> rs1
Uninitialized random effects structure
> str(rs1)
List of 2
 $ Side: Uninitialized positive definite matrix structure of class pdLogChol.
 $ Dog : Uninitialized positive definite matrix structure of class pdLogChol.
 - attr(*, "settings")= int [1:5] 0 1 0 4 4
 - attr(*, "class")= chr "reStruct"
>

I'll suggest adding it, hopefully it will appear in the next update.
In the meantime, you could add the function to your Rprofile,
something like:

con <- file("~/.Rprofile", "a")
writeLines(r"(str.pdMat <- function (object, ...)
{
if (nlme::isInitialized(object)) {
NextMethod()
}
else {
cat(" Uninitialized positive definite matrix structure of class ",
class(object)[1], ".\n", sep = "")
}
})", con)
close(con)

so that every time you start a new R session, this function is added
to your global environment.

On Tue, Nov 15, 2022 at 5:20 PM Iago  wrote:
>
> Dear Bert and all other "helpers",
>
>
> I agree that not all of you are developers. If I look at the DESCRIPTION
> of the nlme package I can see the next:
>
> Contact: see 'MailingList'
>
> BugReports: https://bugs.r-project.org
> MailingList: R-help@r-project.org
>
> Maintainer: R Core Team 
>
> As I do not have bugs.r-project account, first I emailed to
> r-c...@r-project.org, from where I got the next answer
>
> "Non-members are typically*NOT*  allowed to post messages to this
> private developers' list. Please use an appropriate mailing list (from
> http://www.r-project.org/mail.html). For R packages, use
> maintainer("") in R (and if that is R-core@.., use the R-help
> address).
>
> --> i.e. for nlme  use  R-help
>
> by Martin Maechler. Therefore, I used R-help.
>
> Best,
>
> Iago
>
>
> On 15/11/2022 16:20, Bert Gunter wrote:
> > 1. Not developers, helpers (though there may be some developers among
> > us, too). Ergo, we don't make changes to code or man pages either.
> >
> > 2. If no satisfactory reply here, R-Sig-mixed-models is where you
> > should post. And post there first for mixed models questions in future.
> >
> > Cheers,
> > Bert
> >
> >
> > On Mon, Nov 14, 2022 at 11:24 PM IAGO GINÉ VÁZQUEZ 
> > wrote:
> >
> > Dear developers,
> >
> > When I run the example code in the help of reStruct, I get
> >
> >
> > > library(nlme)
> > > rs1 <- reStruct(list(Dog = ~day, Side = ~1), data = Pixel)
> > > rs1
> > Uninitialized random effects structure
> > > str(rs1)
> > List of 2
> >  $ Side:Error in pdMatrix.pdSymm(x) :
> >   cannot extract matrix from an uninitialized object
> >
> >
> > Is it expected?
> >
> > In addition to that I would like to ask if shouldn't be `terms`
> > documented in `?lmeObject`.
> >
> >
> > Kind 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.

__
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] print and lapply....

2022-11-07 Thread Andrew Simmons
I meant something like:

TP <- 1:4
lapply(TP, function(x) {
print(x)
print(x^2)
})

You may wish to add cat("\n") after print(x^2) so that your results
from each iteration are separated.
You may also wish to add invisible() around lapply() if you're not
saving / / using the return list in any way.

On Mon, Nov 7, 2022 at 12:38 PM akshay kulkarni  wrote:
>
> Dear Andrew
>  It doesn't work:
>
> > lapply(TP,function(x){print(x^2)})
> [1] 1
> [1] 4
> [1] 9
> [1] 16
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Basically, lapply() is implemented by a for loop. So there must be some way 
> right?
>
> tHanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Andrew Simmons 
> Sent: Monday, November 7, 2022 10:50 PM
> To: akshay kulkarni 
> Cc: R help Mailing list 
> Subject: Re: [R] print and lapply
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> Dear members,
>  I have the following code and output:
>
> > TP <- 1:4
> > lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at the 
> beginning but before each of x^2?
>
> Many thanks in advance
>
> 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.

__
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] print and lapply....

2022-11-07 Thread Andrew Simmons
put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> Dear members,
>  I have the following code and output:
>
> > TP <- 1:4
> > lapply(TP,function(x){print(x);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> How do I make the print function output x along with x^2, i.e not at the
> beginning but before each of x^2?
>
> Many thanks in advance
>
> 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] Associate a .R file with the RGui

2022-11-04 Thread Andrew Simmons
In an R session, run this:

writeLines(normalizePath(R.home("bin")))

Right click your .R file > Open with > Choose another app > Check the box
"Always use this app to open .R files" > Look for another app on this PC
Paste the directory found above, then select "Rgui.exe"

On Fri, Nov 4, 2022, 04:49 Amarjit Chandhial via R-help <
r-help@r-project.org> wrote:

>
> Hi,
>
>
> My OS is Windows 11 Pro 64-Bit, I have R 4.2.2 and RStudio installed.
>
> If I double-click on a .R file in File Explorer the OS gives me the
> option of opening the .R in RStudio, or Look for an app in the Microsoft
> Store, or More Apps. Similarly with a right-click.
>
> I would like to associate a .R file with the RGui, not RStudio, thus
> when I double-click on a .R file in File Explorer the .R file opens in
> the R Editor in RGui.
>
> On my PC R 4.2.2 is located in "C:/Program Files/R/R-4.2.2/etc"
>
> Please can someone provide step-by-step instructions on how to
> associate?
>
>
> thanks,
> Amarjit
>
> [[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] Unexpected result for df column $ subset with non-existent name

2022-10-30 Thread Andrew Simmons
partial match attr is for something like

attr(data.frame(), "cla")

which will partially match to "class".

On Sun, Oct 30, 2022, 12:55 Joshua Ulrich  wrote:

> For what it's worth, I set these options to warn me about partial matches:
>
> options(warnPartialMatchArgs = TRUE,
> warnPartialMatchAttr = TRUE,
> warnPartialMatchDollar = TRUE)
>
> That warns about this particular case, as well as partial argument
> name matches in function calls. I don't remember what the partial
> match attr does, but past me wanted to be warned about it for some
> reason.
>
> Best,
> Josh
>
> On Fri, Oct 28, 2022 at 9:26 AM Bert Gunter 
> wrote:
> >
> > Does this explain it: (from ?Extract)
> >
> >
> > name
> > A literal character string or a name (possibly backtick quoted). For
> > extraction, this is normally (see under ‘Environments’) **partially
> > matched** to the names of the object.
> >
> > -- Bert
> >
> > On Fri, Oct 28, 2022 at 6:53 AM Sergei Ko  wrote:
> > >
> > > Hi All,
> > >
> > > Just noticed that R returns results for non-existent name if you have
> > > another variable with the same beginning when you subset with $.
> > > See the code below:
> > > name_0 <- "ID"
> > > name_1 <- "name"
> > > name_2 <- "name1"
> > >
> > > v0 <- 1:200
> > > v1 <- c(rep(0,100), rep(1,100))
> > > v2 <- c(rep(0,50), rep(1,150))
> > >
> > > df <- as.data.frame(cbind(v0, v1, v2))
> > > colnames(df) <- c(name_0, name_1, name_2)
> > >
> > > df_1 <- df[, c(name_0, name_1)]
> > > df_2 <- df[, c(name_0, name_2)]
> > >
> > > table(df$name)
> > > table(df_1$name)
> > > table(df_2$name)
> > > colnames(df_2)[2] <- "name10"
> > > table(df_2$name)
> > > colnames(df_2)[2] <- "name_any"
> > > table(df_2$name)
> > > table(df_2[,"name"])
> > >
> > > The last row produces an error as intended.
> > > Any ideas?
> > >
> > > Win 10
> > > R version 4.2.1 (2022-06-23 ucrt) -- "Funny-Looking Kid"
> > >
> > > Regards,
> > > Sergiy
> > >
> > > [[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.
>
>
>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.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.
>

[[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] Partition vector of strings into lines of preferred width

2022-10-28 Thread Andrew Simmons
I would suggest using strwrap(), the documentation at ?strwrap has
plenty of details and examples.
For paragraphs, I would usually do something like:

strwrap(x = , width = 80, indent = 4)

On Fri, Oct 28, 2022 at 5:42 PM Leonard Mada via R-help
 wrote:
>
> Dear R-Users,
>
> text = "
> What is the best way to split/cut a vector of strings into lines of
> preferred width?
> I have come up with a simple solution, albeit naive, as it involves many
> arithmetic divisions.
> I have an alternative idea which avoids this problem.
> But I may miss some existing functionality!"
>
> # Long vector of strings:
> str = strsplit(text, " |(?<=\n)", perl=TRUE)[[1]];
> lenWords = nchar(str);
>
> # simple, but naive solution:
> # - it involves many divisions;
> cut.character.int = function(n, w) {
>  ncm = cumsum(n);
>  nwd = ncm %/% w;
>  count = rle(nwd)$lengths;
>  pos = cumsum(count);
>  posS = pos[ - length(pos)] + 1;
>  posS = c(1, posS);
>  pos = rbind(posS, pos);
>  return(pos);
> }
>
> npos = cut.character.int(lenWords, w=30);
> # lets print the results;
> for(id in seq(ncol(npos))) {
> len = npos[2, id] - npos[1, id];
> cat(str[seq(npos[1, id], npos[2, id])], c(rep(" ", len), "\n"));
> }
>
>
> The first solution performs an arithmetic division on all string
> lengths. It is possible to find out the total length and divide only the
> last element of the cumsum. Something like this should work (although it
> is not properly tested).
>
>
> w = 30;
> cumlen = cumsum(lenWords);
> max = tail(cumlen, 1) %/% w + 1;
> pos = cut(cumlen, seq(0, max) * w);
> count = rle(as.numeric(pos))$lengths;
> # everything else is the same;
> pos = cumsum(count);
> posS = pos[ - length(pos)] + 1;
> posS = c(1, posS);
> pos = rbind(posS, pos);
>
> npos = pos; # then print
>
>
> The cut() may be optimized as well, as the cumsum is sorted ascending. I
> did not evaluate the efficiency of the code either.
>
> But do I miss some existing functionality?
>
>
> Note:
>
> - technically, the cut() function should probably return a vector of
> indices (something like: rep(seq_along(count), count)), but it was more
> practical to have both the start and end positions.
>
>
> Many thanks,
>
>
> Leonard
>
> __
> 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] $ subset operator behavior in lapply

2022-10-27 Thread Andrew Simmons
$ does not evaluate its second argument, it does something like
as.character(substitute(name)).

You should be using

lapply(list, function(x) x$a)

or

lapply(list, `[[`, "a")


On Thu, Oct 27, 2022, 12:29 Hilmar Berger  wrote:

> Dear all,
>
> I'm a little bit surprised by the behavior of the $ operator when used
> in lapply - any indication what might be wrong is appreciated.
>
> > xx = list(A=list(a=1:3, b=LETTERS[1:3]),"B"=list(a=7:9,
> b=LETTERS[7:9]))  > lapply(xx,`$`,"a") $A NULL $B NULL > `$`(xx[[1]],"a")
> [1] 1 2 3 >
> lapply(xx,`[`,"a") $A $A$a [1] 1 2 3 $B $B$a [1] 7 8 9 Any idea why I
> `$`(object, name) works when applied to the single list element but not
> within lapply (in contrast to `[`)?
> I checked the help page of the extraction operators but could not find
> anything that explains this. Thanks and best regards Hilmar >
> sessionInfo() R version 4.2.1 (2022-06-23) Platform: x86_64-pc-linux-gnu
> (64-bit) Running under: Ubuntu 20.04.5 LTS Matrix products: default
> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK:
> /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 locale: [1]
> LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=de_DE.UTF-8
> LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=de_DE.UTF-8
> LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C [9]
> LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=de_DE.UTF-8
> LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices
> utils datasets methods base loaded via a namespace (and not attached):
> [1] compiler_4.2.1
>
> [[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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Andrew Simmons
In the first one, the argument is a character vector of length 1, so the
code works perfectly fine.

The second is a call, and when coerced to a character vector should look
like

c("[", "packages_i_want_to_use", "1")

You can try this yourself with quote(packages_i_want_to_use[1]) which
returns its first argument, unevaluated.

On Mon, Oct 24, 2022, 12:46 Kelly Thompson  wrote:

> Thanks!
>
> # Please, can you help me understand why
> require( 'base' ) # works, but
> require( packages_i_want_to_use[1] ) # does not work?
>
> # In require( 'base' ), what is the "first argument"?
>
> On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons 
> wrote:
> >
> > require(), similarly to library(), does not evaluate its first argument
> UNLESS you add character.only = TRUE
> >
> > require( packages_i_want_to_use[1], character.only = TRUE)
> >
> >
> > On Mon, Oct 24, 2022, 12:26 Kelly Thompson  wrote:
> >>
> >> # Below, when using require(), why do I get the error message "Error
> >> in if (!loaded) { : the condition has length > 1" ?
> >>
> >> # This is my reproducible code:
> >>
> >> #create a vector with the names of the packages I want to use
> >> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
> >>
> >> # Here I get error messages:
> >> require( packages_i_want_to_use[1] )
> >> #Error in if (!loaded) { : the condition has length > 1
> >>
> >> require( packages_i_want_to_use[2] )
> >> #Error in if (!loaded) { : the condition has length > 1
> >>
> >> # Here I get what I expect:
> >> require('base')
> >>
> >> require('this_pac_does_not_exist')
> >> #Loading required package: this_pac_does_not_exist
> >> #Warning message:
> >> #In library(package, lib.loc = lib.loc, character.only = TRUE,
> >> logical.return = TRUE,  :
> >> #  there is no package called ‘this_pac_does_not_exist’
> >>
> >> __
> >> 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.
>

[[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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Andrew Simmons
require(), similarly to library(), does not evaluate its first argument
UNLESS you add character.only = TRUE

require( packages_i_want_to_use[1], character.only = TRUE)


On Mon, Oct 24, 2022, 12:26 Kelly Thompson  wrote:

> # Below, when using require(), why do I get the error message "Error
> in if (!loaded) { : the condition has length > 1" ?
>
> # This is my reproducible code:
>
> #create a vector with the names of the packages I want to use
> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>
> # Here I get error messages:
> require( packages_i_want_to_use[1] )
> #Error in if (!loaded) { : the condition has length > 1
>
> require( packages_i_want_to_use[2] )
> #Error in if (!loaded) { : the condition has length > 1
>
> # Here I get what I expect:
> require('base')
>
> require('this_pac_does_not_exist')
> #Loading required package: this_pac_does_not_exist
> #Warning message:
> #In library(package, lib.loc = lib.loc, character.only = TRUE,
> logical.return = TRUE,  :
> #  there is no package called ‘this_pac_does_not_exist’
>
> __
> 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] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The code working inside stats::weighted.residuals has nothing to do
with being evaluated in a different environment than globalenv() and
has nothing to do with being inside a package.
The reason the code works inside stats::weighted.residuals is because
the function body is wrapped with braces. You can try it yourself:

local({
FILE <- tempfile(fileext = ".R")
on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE,
after = FALSE)
writeLines("if (TRUE) \n'evaluating cons.expr'\nelse
'evaluating alt.expr'", FILE)
writeLines(readLines(FILE))
try(source(FILE, local = TRUE, echo = TRUE, verbose = FALSE))
})

If you try entering it as a function, it still fails:

local({
FILE <- tempfile(fileext = ".R")
on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE,
after = FALSE)
writeLines("function () \nif (TRUE) \n'evaluating
cons.expr'\nelse 'evaluating alt.expr'", FILE)
writeLines(readLines(FILE))
try(source(FILE, local = TRUE, echo = TRUE, verbose = FALSE))
})

But R packages use sys.source() instead of source() to run R code, but
it still fails if you run it:

local({
FILE <- tempfile(fileext = ".R")
on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE,
after = FALSE)
writeLines("if (TRUE) \n'evaluating cons.expr'\nelse
'evaluating alt.expr'", FILE)
writeLines(readLines(FILE))
try(sys.source(FILE, envir = environment()))
})

The part that matters is that the function body is wrapped with
braces. `if` statements inside braces or parenthesis (or possibly
brackets) will continue looking for `else` even after `cons.expr` and
a newline has been fully parsed, but will not otherwise.

On Fri, Oct 21, 2022 at 10:39 AM Jorgen Harmse via R-help
 wrote:
>
> Andrew Simmons is correct but doesn't explain why the code works in the 
> package. This is one of only two differences I have found between running 
> code at the command line and running it from a file. (The other difference is 
> that code in a file is often executed in an environment other than 
> .GlobalEnv. There is some related sugar around packages that usually makes 
> things work the way a user would want.) At the command line, R executes code 
> whenever RETURN could be interpreted as the end of a statement. "If(….) …. 
> RETURN" is ambiguous: will it be followed by "else", or is it a complete 
> statement? If it's in a file or wrapped in a block or other structure that 
> obviously hasn't ended yet then R will wait to see the next line of input, 
> but if it could be a complete statement then not executing it would cause a 
> lot of frustration for users. Once the statement is executed, R expects 
> another statement, and no statement begins with "else". (Maybe the 
> interpreter could be enhanced to keep the "if" open under some conditions, 
> but I haven't thought it through. In particular, "if" without "else" is NULL 
> if the condition is FALSE, so it might be necessary to undo an assignment, 
> and that seems very difficult.)
>
> Regards,
> Jorgen Harmse.
>
>
> On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:
>
> > Hi there,
> >
> > The following code would cause R error:
> >
> >  > w <- 1:5
> >  > r <- 1:5
> >  > if (is.matrix(r))
> > + r[w != 0, , drop = FALSE]
> >  > else r[w != 0]
> > Error: unexpected 'else' in "else"
> >
> > However, the code:
> >  if (is.matrix(r))
> >  r[w != 0, , drop = FALSE]
> >  else r[w != 0]
> > is extracted from stats::weighted.residuals.
> >
> > My question is why the code in the function does not cause error?
> >
> > Best,
> > Jinsong
> >
> > __
> > 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]]
>
>
>
>
> --
>
> Subject: Digest Footer
>
> ___
> 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.
>
>
> --
>
> End of R-help Digest, Vol 236, Issue 19
> ***
>
>

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The error comes from the expression not being wrapped with braces. You
could change it to

if (is.matrix(r)) {
r[w != 0, , drop = FALSE]
} else r[w != 0]

or

{
if (is.matrix(r))
r[w != 0, , drop = FALSE]
else r[w != 0]
}

or

if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

> Hi there,
>
> The following code would cause R error:
>
>  > w <- 1:5
>  > r <- 1:5
>  > if (is.matrix(r))
> + r[w != 0, , drop = FALSE]
>  > else r[w != 0]
> Error: unexpected 'else' in "else"
>
> However, the code:
>  if (is.matrix(r))
>  r[w != 0, , drop = FALSE]
>  else r[w != 0]
> is extracted from stats::weighted.residuals.
>
> My question is why the code in the function does not cause error?
>
> Best,
> Jinsong
>
> __
> 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] Help installing devtools plus other packages in R terminal

2022-10-06 Thread Andrew Simmons
To install the packages from source, you need to install make, gcc, and g++:
⁠sudo apt install make⁠
⁠sudo apt install gcc⁠
⁠sudo apt install g++

then try installing them again

On Thu, Oct 6, 2022 at 2:54 AM Rhon Calderon, Eric
 wrote:
>
> Hi,
>
> I am using R in my HPC terminal. After many tries, I was able to install and 
> exec successfully R on my HPC but now I cannot install devtools and other 
> dependencies. I need some of theses packages to install some others from 
> bioconductor. I was wondering if anybody has a solution.
>
> The script I got from my R session is (the highlighted part is the errors I 
> keep getting):
>
> > install.packages('devtools')
> Installing package into ‘/home/ericrhon/R/x86_64-pc-linux-gnu-library/4.2’
> (as ‘lib’ is unspecified)
> --- Please select a CRAN mirror for use in this session ---
> Secure CRAN mirrors
>
>  1: 0-Cloud [https]
>  2: Australia (Canberra) [https]
>  3: Australia (Melbourne 1) [https]
>  4: Australia (Melbourne 2) [https]
>  5: Australia (Perth) [https]
>  6: Austria [https]
>  7: Belgium (Brussels) [https]
>  8: Brazil (PR) [https]
>  9: Brazil (RJ) [https]
> 10: Brazil (SP 1) [https]
> 11: Brazil (SP 2) [https]
> 12: Bulgaria [https]
> 13: Canada (MB) [https]
> 14: Canada (ON 3) [https]
> 15: Chile (Santiago) [https]
> 16: China (Beijing 2) [https]
> 17: China (Beijing 3) [https]
> 18: China (Hefei) [https]
> 19: China (Hong Kong) [https]
> 20: China (Guangzhou) [https]
> 21: China (Lanzhou) [https]
> 22: China (Nanjing) [https]
> 23: China (Shanghai 2) [https]
> 24: China (Shenzhen) [https]
> 25: Colombia (Cali) [https]
> 26: Costa Rica [https]
> 27: Cyprus [https]
> 28: Czech Republic [https]
> 29: Denmark [https]
> 30: East Asia [https]
> 31: Ecuador (Cuenca) [https]
> 32: Ecuador (Quito) [https]
> 33: France (Lyon 1) [https]
> 34: France (Lyon 2) [https]
> 35: France (Marseille) [https]
> 36: France (Paris 1) [https]
> 37: Germany (Erlangen) [https]
> 38: Germany (Leipzig) [https]
> 39: Germany (Göttingen) [https]
> 40: Germany (Münster) [https]
> 41: Germany (Regensburg) [https]
> 42: Greece [https]
> 43: Hungary [https]
> 44: Iceland [https]
> 45: India [https]
> 46: Indonesia (Banda Aceh) [https]
> 47: Iran (Mashhad) [https]
> 48: Italy (Milano) [https]
> 49: Italy (Padua) [https]
> 50: Japan (Tokyo) [https]
> 51: Japan (Yonezawa) [https]
> 52: Korea (Gyeongsan-si) [https]
> 53: Korea (Ulsan) [https]
> 54: Malaysia [https]
> 55: Mexico (Mexico City) [https]
> 56: Mexico (Texcoco) [https]
> 57: Morocco [https]
> 58: Netherlands (Dronten) [https]
> 59: New Zealand [https]
> 60: Norway [https]
> 61: South Africa (Johannesburg) [https]
> 62: Spain (A Coruña) [https]
> 63: Spain (Madrid) [https]
> 64: Sweden (Borås) [https]
> 65: Sweden (Umeå) [https]
> 66: Switzerland [https]
> 67: Taiwan (Taipei) [https]
> 68: Turkey (Denizli) [https]
> 69: Turkey (Istanbul) [https]
> 70: Turkey (Mersin) [https]
> 71: UK (Bristol) [https]
> 72: UK (London 1) [https]
> 73: USA (IA) [https]
> 74: USA (MI) [https]
> 75: USA (MO) [https]
> 76: USA (OH) [https]
> 77: USA (OR) [https]
> 78: USA (TN) [https]
> 79: USA (TX 1) [https]
> 80: Uruguay [https]
> 81: (other mirrors)
>
> Selection: 1
> also installing the dependencies ‘httpuv’, ‘shiny’, ‘miniUI’
>
> trying URL 'https://cloud.r-project.org/src/contrib/httpuv_1.6.6.tar.gz'
> Content type 'application/x-gzip' length 1875264 bytes (1.8 MB)
> ==
> downloaded 1.8 MB
>
> trying URL 'https://cloud.r-project.org/src/contrib/shiny_1.7.2.tar.gz'
> Content type 'application/x-gzip' length 2982507 bytes (2.8 MB)
> ==
> downloaded 2.8 MB
>
> trying URL 'https://cloud.r-project.org/src/contrib/miniUI_0.1.1.1.tar.gz'
> Content type 'application/x-gzip' length 97958 bytes (95 KB)
> ==
> downloaded 95 KB
>
> trying URL 'https://cloud.r-project.org/src/contrib/devtools_2.4.4.tar.gz'
> Content type 'application/x-gzip' length 374492 bytes (365 KB)
> ==
> downloaded 365 KB
>
> * installing *source* package ‘httpuv’ ...
> ** package ‘httpuv’ successfully unpacked and MD5 sums checked
> ** using staged installation
> ** libs
> g++ -std=gnu++11 -I"/home/ericrhon/R/4.2/R-4.2.1/include" -DNDEBUG 
> -Ilibuv/include -pthread 
> -I'/home/ericrhon/R/4.2/R-4.2.1/library/Rcpp/include' 
> -I'/home/ericrhon/R/4.2/R-4.2.1/library/later/include' -I/usr/local/include  
> -fvisibility=hidden -DSTRICT_R_HEADERS -fpic  -g -O2  -c 
> RcppExports-legacy.cpp -o RcppExports-legacy.o
>
> g++ -std=gnu++11 -I"/home/ericrhon/R/4.2/R-4.2.1/include" -DNDEBUG 
> -Ilibuv/include -pthread 
> -I'/home/ericrhon/R/4.2/R-4.2.1/library/Rcpp/include' 
> -I'/home/ericrhon/R/4.2/R-4.2.1/library/later/include' -I/usr/local/include  
> -fvisibility=hidden -DSTRICT_R_HEADERS -fpic  -g -O2  -c RcppExports.cpp -o 
> RcppExports.o
> g++ -std=gnu++11 

Re: [R] Fatal Error: Contains Space

2022-09-22 Thread Andrew Simmons
Hello,


I'm going to quote the tempdir() doc page: "By default, tmpdir will be the
directory given by tempdir(). This will be a subdirectory of the
per-session temporary directory found by the following rule when the R
session is started. The environment variables TMPDIR, TMP and TEMP are
checked in turn and the first found which points to a writable directory is
used: if none succeeds the value of R_USER (see Rconsole) is used. If the
path to the directory contains a space in any of the components, the path
returned will use the shortnames version of the path."

You need to define one of the environment variables listed above. For
myself, I did something like

TEMP=C:\Users\iris\AppData\Local\Temp


On Thu., Sep. 22, 2022, 03:37 Kaitlyn Light,  wrote:

> Hello!
> I recently downloaded R and RStudio to my windows laptop. I downloaded the
> correct version and made sure it was for windows and not mac. However, when
> I tried to open RStudio, a message saying " R_tempdir Fatal Error:
> Contains Space" would pop-up. The program would open as a blank screen and
> usually make the rest of my laptop stall until it was shut off. I tried
> uninstalling and reinstalling the programs, however, the issue did not
> change. I checked my Microsoft username to make sure it did not "contain
> space" (as in spacebars) but it did not. I was hoping I would be able to
> get some help moving forward on how to get this fixed. Thank you for your
> help!
>
> Best,
> Kaitlyn Light
>
> [[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] S4 dispatch ambiguity

2022-09-21 Thread Andrew Simmons
In the first scenario, your object is class AB, then class A with distance
1, then class B with distance 2. This means that method A is preferable
since it is less distance away than method B.

However, in your second function, both methods are a total distance of 3
away, so (as far as I know) it chooses the first choice in the list and
raises a warning that both are acceptable.

Using contains = c("A", "B") as an ordered set of classes is intended. The
same is true of other multi-inheritance languages, such as python.

I had a similar problem when I was defining methods myself, I had something
like:

row.match <- function (x, table, ...) {}
setGeneric(row.match)
setMethod(row.match, c("data.frame", "ANY"), ...)
setMethod(row.match, c("ANY", "data.frame"), ...)

The easiest thing to do is the define another method like this:
setMethod(row.match, c("data.frame", "data.frame"), ...)

And that removes the ambiguity. For your scenario, I would define two
methods like:
setMethod(`+`, c("A", "A"), ...)
setMethod(`+`, c("B", "B"), ...)

and that should remove your ambiguity.

On Wed., Sep. 21, 2022, 12:06 Xiongtao Dai,  wrote:

> I am trying to make sense why the following does *not* result in
> ambiguous method selection and thus a warning:
>
>  > setClass("A", slots=c(a  = "numeric"))
>  > setClass("B", slots=c(b  = "numeric"))
>  > setClass("AB", contains=c("A", "B"))
>  > setGeneric("myg", function(object) standardGeneric("myg"))
> [1] "myg"
>  >
>  > setMethod("myg", "A", function(object) 1)
>  > setMethod("myg", "B", function(object) 2)
>  > ab <- new("AB", a=1, b=2)
>  > myg(ab)
> [1] 1
>
> On the other hand, the following code gives me a warning
>
>  > setMethod("+", c("A", "B"), function(e1, e2) 1)
>  > setMethod("+", c("B", "A"), function(e1, e2) 2)
>  > ab+ab
> Note: method with signature ‘A#B’ chosen for function ‘+’,
>   target signature ‘AB#AB’.
>   "B#A" would also be valid
> [1] 1
>
> It appears that S4 is using the order of the superclasses A and B for
> dispatching, and that this is not regarded as an ambiguity. Is this the
> expected behavior? I am using R 4.2.1.
>
> It seems this is contradictory to what the documentation of
> methods::setMethod says: "The first possible source of ambiguity arises
> if the class has several direct superclasses and methods have been
> defined for more than one of those; R will consider these equally valid
> and report an ambiguous choice."
>
> Best,
> Xiongtao
>
> __
> 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] How to set default encoding for sourced files

2022-09-21 Thread Andrew Simmons
If you're running it from Rscript, you'll have to specify the encoding like
this:

Rscript --encoding UTF-8 file

If you're using R for Windows, I'm surprised this issue would come up since
R 4.2.0 added support for UTF-8. At least on my own Windows machine, I can
run exactly what you wrote and not have any issues. Are you using an older
version of R?


On Wed., Sep. 21, 2022, 14:20 Andrew Hart via R-help, 
wrote:

> On 21/09/2022 11:46, Bert Gunter wrote:
> > ?options
> >
> > options(encoding = "utf-8")
> > in a startup file or function should presumably do it. See ?Startup
> >
> > Bert
>
> Thanks everyone. Setting encoding in options in Rprofile.site has taken
> care of it.
>
> Curiously, it doesn't seem to solve the whole problem for Rscript
> though. I checked that Rscript is indeed picking up the default encoding
> from options, but it's complaining about seeing an unexpected input in
> dat$línea <- 
> immediately following the l when I run
> Rscript myfile.R.
> So, it would appear that Rscript is not using source to read in the R
> script. Mind you, if I do
> Rscript -e source('myfile.R')
> it works properly just like in Rgui.
>
> Once again, Thanks heaps.
>
> Cheers,
> Andrew.
>
> __
> 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] Error in if (class(networks) == "matrix") from a function

2022-09-21 Thread Andrew Simmons
In general, you should be using inherits(netwotks, "matrix") or
is(networks, "matrix") instead of class() ==

Your function fails because your object has multiple classes so class==
returns multiple logical values so if will fail.

But inherits or is will return one logical value, so if will not raise an
error.

On Wed., Sep. 21, 2022, 15:21 Chao Liu,  wrote:

> Dear R-Help community,
>
> This is a crosspost on SO but I've had no luck so far. So I have a function
> which computes a centrality index for the nodes in a network or matrix.
> Here is the function:
>
> library(igraph) #load package igraph
> centrality <- function (networks, type = c("indegree", "outdegree",
> "freeman",
> "betweenness", "flow", "closeness", "eigenvector", "information",
> "load", "bonpow"), directed = TRUE, lag = 0, rescale = FALSE,
> center = FALSE, coefname = NULL, ...) {
> if (is.null(directed) || !is.logical(directed)) {
> stop("'directed' must be TRUE or FALSE.")
> }
> else if (length(directed) != 1) {
> stop("The 'directed' argument must contain a single logical
> value only.")
> }
> else if (directed == FALSE) {
> gmode <- "graph"
> }
> else {
> gmode <- "digraph"
> }
> objects <- checkDataTypes(y = NULL, networks = networks,
> lag = lag)
> centlist <- list()
> for (i in 1:objects$time.steps) {
> if (type[1] == "indegree") {
> cent <- degree(objects$networks[[i]], gmode = gmode,
> cmode = "indegree", rescale = rescale, ...)
> }
> else if (type[1] == "outdegree") {
> cent <- degree(objects$networks[[i]], gmode = gmode,
> cmode = "outdegree", rescale = rescale, ...)
> }
> else if (type[1] == "freeman") {
> cent <- degree(objects$networks[[i]], gmode = gmode,
> cmode = "freeman", rescale = rescale, ...)
> }
> else if (type[1] == "betweenness") {
> cent <- betweenness(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "flow") {
> cent <- flowbet(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "closeness") {
> cent <- closeness(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "eigenvector") {
> cent <- evcent(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "information") {
> cent <- infocent(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "load") {
> cent <- loadcent(objects$networks[[i]], gmode = gmode,
> rescale = rescale, ...)
> }
> else if (type[1] == "bonpow") {
> cent <- bonpow(objects$networks[[i]], gmode = gmode,
> rescale = rescale, tol = 1e-20, ...)
> }
> else {
> stop("'type' argument was not recognized.")
> }
> centlist[[i]] <- cent
> }
> time <- numeric()
> y <- numeric()
> for (i in 1:objects$time.steps) {
> time <- c(time, rep(i, objects$n[[i]]))
> if (is.null(centlist[[i]])) {
> y <- c(y, rep(NA, objects$n[[i]]))
> }
> else {
> if (center == TRUE) {
> centlist[[i]] <- centlist[[i]] - mean(centlist[[i]],
>   na.rm = TRUE)
> }
> y <- c(y, centlist[[i]])
> }
> }
> if (is.null(coefname) || !is.character(coefname) || length(coefname) >
> 1) {
> coeflabel <- ""
> }
> else {
> coeflabel <- paste0(".", coefname)
> }
> if (lag == 0) {
> laglabel <- ""
> }
> else {
> laglabel <- paste0(".lag", paste(lag, collapse = "."))
> }
> label <- paste0(type[1], coeflabel, laglabel)
> dat <- data.frame(y, time = time, node = objects$nodelabels)
> dat$node <- as.character(dat$node)
> colnames(dat)[1] <- label
> attributes(dat)$lag <- lag
> return(dat)}
>
> Here is the matrix:
>
> dat <- read.table(text="A B #this is edgelist
> 1 2
> 1 3
> 1 2
> 2 1
> 2 3
> 3 1
> 3 2
> 3 1", header=TRUE)
> datmat <- as.matrix(get.adjacency(graph.edgelist(as.matrix(dat),
> directed=TRUE))) #this is the matrix
> colnames(datmat) <- c("1", "2", "3") #rename the columns
>
> When I applied the function to a matrix
> centrality(datmat,type="flow",center=TRUE), it returns the error:
>
> Error in if (class(networks) == "matrix") { :
>   the condition has length > 1
>
> What is the cause of this error? How to fix it? Any help will be greatly
> appreciated!
>
>
> Best,
>
> Chao
>
> [[alternative HTML version deleted]]
>
> 

[R] Returning Visibly / / Invisibly from C Function

2022-09-16 Thread Andrew Simmons
Hello,


I'm working on a function envvars() which I'd like to get, set, and
remove environment variables in a manner similar to options(). At the
R level, I have this function:

envvars <- function (...)
.External2(C_envvars, pairlist(...))

and then at the C level:

#define set_R_Visible(X) (eval( (X) ? R_NilValue :
lang1(install("invisible")) , R_BaseEnv))

SEXP do_envvars(SEXP call, SEXP op, SEXP args, SEXP rho)
{
// details omitted
set_R_Visible(visible);  /* 0 or 1 for invisible or visible return */
UNPROTECT(nprotect);
return value;
}

This seems to work as intended, it returns invisibly when getting 0
environment variables (setting/unsetting are not counted as getting)
and returns visibly when getting >= 1 environment variables.

My concern is that this solution feels hack-ish, like it could easily
break in a future update. So, does anyone know of a more official way
to change the visibility of a return value from a C function? 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] inadequacy in as.integer....

2022-09-11 Thread Andrew Simmons
What you're asking for doesn't make sense: 9098 and 09098 are the same

9098L == 09098L

If you mean specifically while printing, you could use sprintf:

cat(sprintf("%05d", 9098))

On Sun., Sep. 11, 2022, 14:58 akshay kulkarni, 
wrote:

> Dear Tim,
>   So there is no way to coerce "09098" to 09098?
>
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Ebert,Timothy Aaron 
> Sent: Monday, September 12, 2022 12:07 AM
> To: akshay kulkarni ; R help Mailing list <
> r-help@r-project.org>
> Subject: RE: inadequacy in as.integer
>
> Keep as character.
>
> Tim
>
> -Original Message-
> From: R-help  On Behalf Of akshay kulkarni
> Sent: Sunday, September 11, 2022 12:22 PM
> To: R help Mailing list 
> Subject: [R] inadequacy in as.integer
>
> [External Email]
>
> Dear members,
>  I came across this queer thing during my
> analysis:
> > as.integer("09098")
>
>
> Any idea on how to retain the "0"?
>
> Yours sincerely,
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C70a6ae074d1e42c17e9108da9411d934%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637985101625267716%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=%2FV4NoU8j%2F9KIYX5ExaY93fwQf%2FPqx1kisbt87Mj9%2F7Q%3Dreserved=0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C70a6ae074d1e42c17e9108da9411d934%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637985101625267716%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=BA0o8oGcR%2FFBX9Vth5DuitlZnTGG7rNDmFga3ixZ9j4%3Dreserved=0
> 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.
>

[[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] inconsistency in switch statements.....

2022-09-07 Thread Andrew Simmons
1 and 2 are not valid identifiers in R, so you need to surround them with
backticks to make them valid, the same as quoting a string:

switch(Stst, `1` = print("NO"), `2` = print("YES"))


On Wed., Sep. 7, 2022, 14:35 akshay kulkarni,  wrote:

> Dear members,
>  The following is my code:
>
> > Stst <- 2
> > switch(Stst, 1 = print("NO"), 2 = print("YES"))
> Error: unexpected '=' in "switch(Stst, 1 ="
>
> Why isn't it printing "YES" on to the console?
>
> many thanks in advance.
>
> 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] expand a matrix

2022-09-05 Thread Andrew Simmons
You can specify multiple indexes to replace at once, so you can avoid
a for loop entirely like this:
M <- matrix(nrow = 10, ncol = 10)
M[1:4, 5: 6] <- M[5: 6, 1:4] <- m[1, 2]
M[1:4,7] <- M[   7, 1:4] <- m[1, 3]
M[1:4, 8:10] <- M[8:10, 1:4] <- m[1, 4]
M[5:6,7] <- M[   7, 5:6] <- m[2, 3]
M[5:6, 8:10] <- M[8:10, 5:6] <- m[2, 4]
M[  7, 8:10] <- M[8:10,   7] <- m[3, 4]
M


You could also specify it directly with function 'matrix', it is
somewhat ugly though:
M <- matrix(c(
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
m[1,2], m[1,2], m[1,2], m[1,2], NA, NA, m[2,3], m[2,4],
m[2,4], m[2,4],
m[1,2], m[1,2], m[1,2], m[1,2], NA, NA, m[2,3], m[2,4],
m[2,4], m[2,4],
m[1,3], m[1,3], m[1,3], m[1,3], m[2,3], m[2,3], NA, m[3,4],
m[3,4], m[3,4],
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA,
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA,
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA
), nrow = 10, ncol = 10, byrow = TRUE)
M


If it interests you, you could take a look at package 'Matrix', it
contains classes for specific types of matrices, including upper
triangular matrices. This package helps to improve speed of linear
algebra computations and reduce the memory size of matrices.

On Mon, Sep 5, 2022 at 10:46 PM Jinsong Zhao  wrote:
>
> Hi there,
>
> I have a matrix likes:
>  > m
>   [,1] [,2] [,3] [,4]
> [1,]   NA123
> [2,]1   NA65
> [3,]26   NA4
> [4,]354   NA
>
> I hope to expand it to 10 by 10 matrix, M, likes:
>  > M
>[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>   [1,]   NA   NA   NA   NA11233 3
>   [2,]   NA   NA   NA   NA11233 3
>   [3,]   NA   NA   NA   NA11233 3
>   [4,]   NA   NA   NA   NA11233 3
>   [5,]1111   NA   NA655 5
>   [6,]1111   NA   NA655 5
>   [7,]222266   NA44 4
>   [8,]3333554   NA   NANA
>   [9,]3333554   NA   NANA
> [10,]3333554   NA   NANA
>
> I use the following code:
>
> M <- matrix(NA, 10, 10)
>
> for (i in 1:10) {
> for (j in 1:10) {
>if (i %in% 1:4 & j %in% 5:6) M[i,j] <- M[j,i] <- m[1,2]
>if (i %in% 1:4 & j == 7) M[i,j] <- M[j,i] <-m[1,3]
>if (i %in% 1:4 & j %in% 8:10) M[i,j] <- M[j,i] <-m[1,4]
>if (i %in% 5:6 & j == 7) M[i,j] <- M[j,i] <-m[2,3]
>if (i %in% 5:6 & j %in% 8:10) M[i,j] <- M[j,i] <-m[2,4]
>if (i == 7 & j %in% 8:10) M[i,j] <- M[j,i] <-m[3,4]
> }
> }
>
> Is there any convenience way to do it? Thanks!
>
> Best,
> Jinsong
>
> __
> 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] Read excel specific column

2022-08-23 Thread Andrew Simmons
I like package openxlsx, with the function openxlsx::read.xlsx()

Another common package that people use readxl

On Tue., Aug. 23, 2022, 7:51 p.m. Anas Jamshed, 
wrote:

> I have .xlsx files with gene names in first column.How can read and load in
> R?
>
> [[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] grep

2022-07-10 Thread Andrew Simmons
?regex is a nice starting point, it's got plenty of details on meta
characters and characters classes, if you need more advanced stuff you'll
probably have to look at the perl regex documentation, I believe it's
linked in ?regex.

I might try something like

grep("^[xz]\\.")

or

grep("^[xz][.]")

or if you'd consider a different function,

startsWith(jj, "x.") | startsWith(jj, "z.")

On Sun, Jul 10, 2022, 21:49 Steven T. Yen  wrote:

> Thanks Jeff. It works. If there is a good reference I should read
> (besides ? grep) I's be glad to have it.
>
> On 7/11/2022 9:30 AM, Jeff Newmiller wrote:
> > grep( "^(z|x)\\.", jj, value = TRUE )
> >
> > or
> >
> > grep( r"(^(z|x)\.)", jj, value = TRUE )
> >
> >
> > On July 10, 2022 6:08:45 PM PDT, "Steven T. Yen" 
> wrote:
> >> Dear, Below, jj contains character strings starting with “z.” and “x.”.
> I want to grep all that contain either “z.” or “x.”. I had to grep “z.” and
> “x.” separately and then tack the result together. Is there a convenient
> grep option that would grep strings with either “z.” or “x.”. Thank you!
> >>
> >>> jj<-names(v$est); jj
> >>   [1] "z.one" "z.liberal" "z.conserv" "z.dem" "z.rep"
> "z.realinc"
> >>   [7] "x.one" "x.liberal" "x.conserv" "x.dem" "x.rep"
> "x.realinc"
> >> [13] "mu1_1" "mu2_1" "rho"
> >>> j1<-grep("z.",jj,value=TRUE); j1
> >> [1] "z.one" "z.liberal" "z.conserv" "z.dem" "z.rep" "z.realinc"
> >>> j2<-grep("x.",jj,value=TRUE); j2
> >> [1] "x.one" "x.liberal" "x.conserv" "x.dem" "x.rep" "x.realinc"
> >>> j<-c(j1,j2); j
> >>   [1] "z.one" "z.liberal" "z.conserv" "z.dem" "z.rep"
> "z.realinc"
> >>   [7] "x.one" "x.liberal" "x.conserv" "x.dem" "x.rep"
> "x.realinc"
> >>
> >> __
> >> 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.
>

[[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] Obtaining the source code

2022-06-19 Thread Andrew Simmons
You can use getAnywhere

On Sun, Jun 19, 2022, 13:23 Christofer Bogaso 
wrote:

> Hi,
>
> I am trying to see the source code of rstandard function. I tried below,
>
> > methods('rstandard')
>
> [1] rstandard.glm* rstandard.lm*
>
> What do I need to do if I want to see the source code of rstandard.lm*?
>
> Thanks for your help.
>
> __
> 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] How to improve accuracy of output?

2022-05-24 Thread Andrew Simmons
I think you have to use print(df$price, digits = 14)

On Tue, May 24, 2022, 10:01 maithili_shiva--- via R-help <
r-help@r-project.org> wrote:

> Dear Forum
> In my code, I am trying to compute Value at risk.
> I have an issue as mentioned below:
> I have defined
>
>
> options(digits = 14)
> But my output is something like
> >  df$price
> [1] 98.7185273 103.10265923  
> However, if use Excel, I get values as
> 98.718527356108 103.10265923704
> and so on.
> Is there any way I can improve on R output accuracy.
> Problem is If I use
> options(digits = 20)
> I still get same prices as when I was using options(digits = 14) and on
> the other hand my confidence level value, which should be equal to 0.99,
> gets distorted to 0.9899 impacting  Value at Risk value.
> Please guide
> Regards
> Maithreyi
> [[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] Somewhat disconcerting behavior of seq.int()

2022-05-02 Thread Andrew Simmons
A sequence where 'from' and 'to' are both integer valued (not necessarily
class integer) will use R_compact_intrange; the return value is an integer
vector and is stored with minimal space.

In your case, you specified a 'from', 'to', and 'by'; if all are integer
class, then the return value is also integer class. I think if 'from' and
'to' are integer valued and 'by' is integer class, the return value is
integer class, might want to check that though. In your case, I think
replacing 'by = 1' with 'by = 1L' will mean the sequences are identical,
though it may still take longer than not specifying at all.

On Mon, May 2, 2022, 21:46 Bert Gunter  wrote:

> ** Disconcerting to me, anyway; perhaps not to others**
> (Apologies if this has been discussed before. I was a bit nonplussed by
> it, but maybe I'm just clueless.) Anyway:
>
> Here are two almost identical versions of the Sieve of Eratosthenes.
> The difference between them is only in the call to seq.int() that is
> highlighted
>
> sieve1 <- function(m){
>if(m < 2) return(NULL)
>a <- floor(sqrt(m))
>pr <- Recall(a)
> 
>s <- seq.int(2, to = m) ## Only difference here
> ##
>for( i in pr) s <- s[as.logical(s %% i)]
>c(pr,s)
> }
>
> sieve2 <- function(m){
>if(m < 2) return(NULL)
>a <- floor(sqrt(m))
>pr <- Recall(a)
> 
>s <- seq.int(2, to = m, by =1) ## Only difference here
> ###
>for( i in pr) s <- s[as.logical(s %% i)]
>c(pr,s)
> }
>
> However, execution time is *quite* different.
>
> library(microbenchmark)
>
> > microbenchmark(l1 <- sieve1(1e5), times =50)
> Unit: milliseconds
> expr  min   lq mean  median   uq  max
>  l1 <- sieve1(1e+05) 3.957084 3.997959 4.732045 4.01698 4.184918 7.627751
>  neval
> 50
>
> > microbenchmark(l2 <- sieve2(1e5), times =50)
> Unit: milliseconds
> expr  min  lq mean   median   uq  max
>  l2 <- sieve2(1e+05) 681.6209 682.555 683.8279 682.9368 685.2253 687.9464
>  neval
> 50
>
> Now note that:
> > identical(l1, l2)
> [1] FALSE
>
> ## Because:
> > str(l1)
>  int [1:9592] 2 3 5 7 11 13 17 19 23 29 ...
>
> > str(l2)
>  num [1:9592] 2 3 5 7 11 13 17 19 23 29 ...
>
> I therefore assume that seq.int(), an internal generic, is dispatching
> to a method that uses integer arithmetic for sieve1 and floating point
> for sieve2. Is this correct? If not, what do I fail to understand? And
> is this indeed the source of the large difference in execution time?
>
> Further, ?seq.int says:
> "The interpretation of the unnamed arguments of seq and seq.int is not
> standard, and it is recommended always to name the arguments when
> programming."
>
> The above suggests that maybe this advice should be qualified, and/or
> adding some comments to the Help file regarding this behavior might be
> useful to naïfs like me.
>
> In case it makes a difference (and it might!):
>
> > sessionInfo()
> R version 4.2.0 (2022-04-22)
> Platform: x86_64-apple-darwin17.0 (64-bit)
> Running under: macOS Monterey 12.3.1
>
> Matrix products: default
> LAPACK:
> /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> other attached packages:
> [1] microbenchmark_1.4.9
>
> loaded via a namespace (and not attached):
> [1] compiler_4.2.0 tools_4.2.0
>
>
> Thanks for any enlightenment and again apologies if I am plowing old
> ground.
>
> Best to all,
>
> Bert Gunter
>
> __
> 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] Time Zone problems: midnight goes in; 8am comes out

2022-03-01 Thread Andrew Simmons
It seems like the current version of lubridate is 1.8.0, which does
raise a warning for an invalid timezone, just like as.POSIXct. This is
what I tried:


print(lubridate::parse_date_time("1970-01-01 00:01:00",  "ymd
HMS"  , tz = "PST"))
print(as.POSIXct("1970-01-01 00:01:00", format =
"%Y-%m-%d %H:%M:%S", tz = "PST"))


outputs:


> print(lubridate::parse_date_time("1970-01-01 00:01:00",  "ymd HMS"
>   , tz = "PST"))
[1] "1970-01-01 08:01:00 GMT"
Warning message:
In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'PST'
> print(as.POSIXct("1970-01-01 00:01:00", format = "%Y-%m-%d 
> %H:%M:%S", tz = "PST"))
[1] "1970-01-01 00:01:00 GMT"
Warning messages:
1: In strptime(x, format, tz = tz) : unknown timezone 'PST'
2: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
  unknown timezone 'PST'
3: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'PST'
>


But I don't see the same problem when using `tz =
"America/Los_Angeles"` or `tz = "PST8PDT"`.


print(lubridate::parse_date_time("1970-01-01 00:01:00",  "ymd
HMS"  , tz = "PST8PDT"))
print(as.POSIXct("1970-01-01 00:01:00", format =
"%Y-%m-%d %H:%M:%S", tz = "PST8PDT"))


outputs:


> print(lubridate::parse_date_time("1970-01-01 00:01:00",  "ymd HMS"
>   , tz = "PST8PDT"))
[1] "1970-01-01 00:01:00 PST"
> print(as.POSIXct("1970-01-01 00:01:00", format = "%Y-%m-%d 
> %H:%M:%S", tz = "PST8PDT"))
[1] "1970-01-01 00:01:00 PST"
>


I would hesitate to use `tz = Sys.timezone()` because someone from
another province/state might not be able to use your code. Depends on
whether this work is being shared with other people though, up to you.

On Tue, Mar 1, 2022 at 8:51 PM Boylan, Ross via R-help
 wrote:
>
> I'm having problems with timezones using lubridate, but it's not clear to me 
> the difficulty is in lubridate.
> -
> > r2 <- parse_date_time("1970-01-01 00:01:00", "ymd HMS", tz="PST")
> > r2
> [1] "1970-01-01 08:01:00 PST"  ## Oops: midnight has turned in 8am
> > as.numeric(r2)
> [1] 28860
> > 8*3600 # seconds in 8 hours
> [1] 28800
> 
> lubridate accepts PST as the time zone, and the result prints "PST" for 
> timezone.  Further, lubridate seems to be using the tz properly since it gets 
> the 8 hour offset from UTC correct.
>
> The problem is the value that is printed gives a UTC time of 08:01 despite 
> having the PST suffix.  So the time appears to have jumped 8 hours ahead from 
> the value parsed.
>
> PST appears not to be a legal timezone (in spite of lubridate inferring the 
> correct offset from it):
> ---
> > Sys.timezone()
> [1] "America/Los_Angeles"
>
> > (grep("PST", OlsonNames(), value=TRUE))
> [1] "PST8PDT" "SystemV/PST8""SystemV/PST8PDT"
> -
> https://www.r-bloggers.com/2018/07/a-tour-of-timezones-troubles-in-r/ says 
> lubridate will complain if given an invalid tz, though I don't see that 
> explicitly in the current man page 
> https://lubridate.tidyverse.org/reference/parse_date_time.html.  As shown 
> above, parse_date_time() does not complain about the timezone, and does use 
> it to get the correct offset.
>
> Using America/Los_Angeles produces the expected results:
> ---
> > r4 <- parse_date_time("1970-01-01 00:01:00", "ymd HMS", tz=Sys.timezone())
> > r4
> [1] "1970-01-01 00:01:00 PST"  # still prints PST.  This time it's true!
> > as.numeric(r4)
> [1] 28860
> 
>
> I suppose I can just use "America/Los_Angeles" as the time zone; this would 
> have the advantage of making all my timezones the same, which apparently what 
> R requires for a vector of datetimes.  But the behavior seems odd, and the 
> "fix" also requires me to ignore the time zone specified in my inputs, which 
> look like "2022-03-01 15:54:30 PST" or PDT, depending on time of year.
>
> 1. Why this strange behavior in which PST or PDT is used to construct the 
> proper offset from UTC, and then kind of forgotten on output?
> 2. Is this a bug in lubridate or base POSIXct, particularly its print routine?
>
> My theory on 1 is that lubridate understands PST and constructs an 
> appropriate UTC time.  POSIXct time does not understand a tz of "PST" and so 
> prints out the UTC value for the time, "decorating" it with the not 
> understood tz value.
>
> For 2, on one hand, lubridate is constructing POSIXct dates with invalid tz 
> values; lubridate probably shouldn't.  On the other hand, POSIXct is printing 
> a UTC time but labeling it with a tz it doesn't understand, so it looks if 
> it's in that local time even though it isn't.  In the context above that 
> seems like a bug, but it's possible a lot of code that depends on it.
>
> Under these theories, the problems only arise because the set of tz values 

Re: [R] Why does the print method fail for very small numbers?

2022-02-17 Thread Andrew Simmons
It should also be noted that format(x, digits = 17) instead of
as.character(x) won't lose any accuracy.

On Thu, Feb 17, 2022, 17:41 Marius Hofert 
wrote:

> Dear expeRts,
>
> I'm familiar with IEEE 754. Is there an easy way to explain why even
> just printing of small numbers fails?
>
> 1e-317 # 1e-317 => fine
> 1e-318 # 9.87e-319 => gets tricky; seems to call print() =>
> as.character() => format() => paste()
> 1e-318 == 9.87e-319 # TRUE
> 2.48e-324 # prints 4.940656e-324 for me
> 2.48e-324 == 4.940656e-324 # TRUE
> ## Relative error as a plot
> rel_error <- function(x)
> plot(abs((as.numeric(as.character(x)) - x) / x), type = "l",
>  ylab = "Relative error between x and as.numeric(as.character(x))")
> rel_error(seq(0.001, 0.001 + .Machine$double.xmin, length.out = 1001)) #
> fine
> rel_error(seq(0, .Machine$double.xmin, length.out = 1001)) # printing
> breaks down
>
> Of course, [0,.Machine$double.xmin] is somewhat of a strange set of
> numbers to consider,
> and I expect things like "==" to be easily fooled there, but already the
> print method (?)
>
> Thanks & cheers,
> Marius
>
> sessionInfo()
> R version 4.1.2 (2021-11-01)
> Platform: x86_64-apple-darwin21.2.0 (64-bit)
> Running under: macOS Monterey 12.1
> ...
>
>
> __
> 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] Help with developing package DWLS

2022-01-11 Thread Andrew Simmons
The NOTE saying 'Possibly misspelled words in DESCRIPTION' can probably be
ignored (though I would probably put the name of your package in single
quotes).


The NOTE 'Non-standard files/directories found at top level' means that you
should move the non-standard files to a different location OR add the
files/directories to your .Rbuildignore file
For example, in my own packages, I usually have a script that will
conveniently run R CMD build, R CMD INSTALL, and R CMD check on that
package, but I don't want to include it in the *.tar.gz, so I add the line:
^Check_This\.R$
to the .Rbuildignore file, and then it's not included. For you, adding
something like:
^Reference_Manual_DWLS\.md$
^Reference_Manual_Rd2md\.md$
might work.


The NOTE 'DEAnalysisMAST: no visible binding for global variable
  'Number.of.Cells'', basically on line 46 of the function you have
something like vbeta.1 <- subset(vbeta.fa, Number.of.Cells == 1)
Anyone reading this would know that Number.of.Cells == 1 is evaluated
within the context vbeta.fa, but R CMD check doesn't, so something like:
vbeta.1 <- vbeta.fa[vbeta.fa[["Number.of.Cells"]] == 1, , drop = FALSE]
might get around this issue while still behaving the same code-wise.


The WARNING 'Undocumented code objects:', even though these objects you've
exported are not functions, they still need a documentation file.
It seems like you're using roxygen for documentation, I'm not familiar with
that, but I'd imagine it's nearly identical to documenting a function.
Here's the chapter of the manual they were referring to:
https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files


The NOTE 'installed size is 66.4Mb'
and WARNING 'LazyData DB of 66.3 MB without LazyDataCompression set'
basically, you have too much data. Or not too much necessarily, but it's
not compressed enough.
The default compression is gzip, but you can also try bzip2 and xz.
You can do this by adding the argument --data-compress=bzip2
or --data-compress=xz to R CMD INSTALL,
or you can add LazyDataCompression: bzip2 or LazyDataCompression: xz to
your DESCRIPTION file.
It seems like you already have the line LazyDataCompression: xz in your
DESCRIPTION, but it's commented out, might want to un-comment it.
Also, for comparisons sake, you could add '-1' to the package version
and LazyDataCompression:
gzip, and then do the same thing adding '-2' and LazyDataCompression:
bzip2, and '-3' and LazyDataCompression: xz so that you figure out which
has the best compression.
You can also visit here for more details:
https://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages


The NOTE 'Examples with CPU (user + system) or elapsed time > 10s'
basically, your examples need to not take as long. If you'd like, you could
move the long computations into a demo, see here:
https://cran.r-project.org/doc/manuals/R-exts.html#Package-subdirectories
Or you could also add a question for the user:
if (utils::askYesNo("Would you like to run the longer, more involved
example?", default = interactive())) {
# long example
} else {
# make a shorter example here
}
and this way, when being check by R CMD check, it will run the short
example, while still allowing the user to run the long example.


I don't think I can offer much help with the last ERROR,


Might also be worth noting, my Windows machine brings up some things that
yours doesn't. For example, it says the directory should be named 'data',
not 'Data'.
Here's the rest of the print out:
andre@DESKTOP-VR312SR:~$ R CMD "check" "--as-cran"
"C:\Users\andre\AppData\Local\Temp\RtmpcjdmpU\file4b8c681a309f\DWLS_0.1.0.tar.gz"
* using log directory 'C:/Users/andre/Documents/DWLS.Rcheck'
* using R version 4.1.2 (2021-11-01)
* using platform: x86_64-w64-mingw32 (64-bit)
* using session charset: ISO8859-1
* using option '--as-cran'
* checking for file 'DWLS/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'DWLS' version '0.1.0'
* package encoding: UTF-8
* checking CRAN incoming feasibility ... NOTE
Maintainer: 'Adriana Sistig '

New submission

Unknown, possibly mis-spelled, fields in DESCRIPTION:
  'RemoteType' 'RemoteHost' 'RemoteRepo' 'RemoteUsername' 'RemoteRef'
'RemoteSha' 'GithubRepo' 'GithubUsername' 'GithubRef' 'GithubSHA1'

Size of tarball: 32169874 bytes
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking whether package 'DWLS' can be installed ... OK
* checking installed package size ... NOTE
  installed size is 66.4Mb
  sub-directories of 1Mb or more:
data  66.3Mb
* checking package directory ... OK
* checking for future file timestamps ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... NOTE
Non-standard file/directory found at top 

Re: [R] question about for loop

2021-12-24 Thread Andrew Simmons
y, c, and f only exist in the context of mac2
If you want to use them, you'll have to write mac2$y, mac2$c, or mac2$f (or
the [[ versions mac2[["y"]], mac2[["c"]], or mac2[["f"]])
Combining that with index i would then look like mac2$y[[i]] or mac2[[i,
"y"]]

Also, I think you want to use aes_string instead of aes (since you want
those expressions within aes to be evaluated)
Something like this seems to work for me:


`%>%` <- magrittr::`%>%`


writeLines(FILE <- tempfile(), text =
r"{y,c,f
hwy,cyl,hwy_cyl2
cty,class,cty_class2}")


mac2 <- readr::read_csv(FILE)
for (i in seq_len(nrow(mac2))) {
ggplt <- ggplot2::mpg %>%
dplyr::filter(hwy < 35) %>%
ggplot2::ggplot(
ggplot2::aes_string(
x = "displ",
y = mac2[[i, "y"]],
color = mac2[[i, "c"]]
)
) +
ggplot2::geom_point() +
ggplot2::ylab(mac2[[i, "y"]]) +
ggplot2::guides(
color = ggplot2::guide_legend(title = mac2[[i, "c"]])
)
ggplot2::ggsave(
filename = tempfile(
mac2[[i, "f"]],
fileext = ".jpg"
),
plot = ggplt,
width = 9, height = 6, dpi = 1200
)
}


unlink(FILE)


runs fine on my computer, but might look more like this for you:


library(magrittr)
library(ggplot2)
library(dplyr)
library(readr)


mac2 <- read_csv("C:/temp/mac2.csv")
for (i in seq_len(nrow(mac2))) {
ggplt <- mpg %>%
filter(hwy < 35) %>%
ggplot(
aes_string(
x = "displ",
y = mac2[[i, "y"]],
color = mac2[[i, "c"]]
)
) +
geom_point() +
ylab(mac2[[i, "y"]]) +
guides(
color = guide_legend(title = mac2[[i, "c"]])
)
ggsave(
filename = paste0("C:/temp/", mac2[[i, "f"]], ".jpg"),
plot = ggplt,
width = 9, height = 6, dpi = 1200
)
}


try reading through aes and aes_string, and keep in mind that columns in
data frames aren't R variables (where they are in Excel). If you want to
use columns like they are variables, you can try using `with`. For example:


library(magrittr)
library(ggplot2)
library(dplyr)
library(readr)


mac2 <- read_csv("C:/temp/mac2.csv")
for (i in seq_len(nrow(mac2))) {
with(mac2[i, ], {
ggplt <- mpg %>%
filter(hwy < 35) %>%
ggplot(
aes_string(
x = "displ",
y = y,
color = c
)
) +
geom_point() +
ylab(y) +
guides(
color = guide_legend(title = c)
)
ggsave(
filename = paste0("C:/temp/", f, ".jpg"),
plot = ggplt,
width = 9, height = 6, dpi = 1200
)
})
}




On Fri, Dec 24, 2021 at 4:48 PM Kai Yang via R-help 
wrote:

> Hello Team,
> I create a csv file (mac2) to save parameter values. the file looks like:
>
> y,c,f
> hwy,cyl,hwy_cyl2
> cty,class,cty_class2
>
> Then I load the file into R and apply the parameters y, c, f in for loop,
> see my code below:
> library(ggplot2)
> library(tidyverse)
> library(readr)
> mac2 <- read_csv("C:/temp/mac2.csv")
> View(mac2)
> for (i in seq(nrow(mac2))){
>   mpg %>%
> filter(hwy <35) %>%
> ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]) )) +
> geom_point()+
> ylab(y[i]) +
> guides(color = guide_legend(title = c[i]))
> ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200,
> units = "in")
> }
>
> but I got an error message: "Error in dots_list(..., title = title,
> subtitle = subtitle, caption = caption,  :  object 'y' not found"
> Does anyone know how to fix the problem?
> Thanks,
> Kai
>
>
> [[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] for loop question in R

2021-12-22 Thread Andrew Simmons
nrow() is just the numbers of rows in your data frame, use seq_len(nrow())
or seq(nrow()) to loop through all row numbers

On Wed, Dec 22, 2021, 12:08 Kai Yang via R-help 
wrote:

> Hello R team,I want to use for loop to generate multiple plots with 3
> parameter, (y is for y axis, c is for color and f is for file name in
> output). I created a data frame to save the information and use the
> information in for loop. I use y[i], c[i] and f[i] in the loop, but it
> seems doesn't work. Can anyone correct my code to make it work?
> Thanks,Kai
>
> library(ggplot2)library(tidyverse)
> y <- c("hwy","cty")c <- c("cyl","class")f <- c("hwy_cyl","cty_class")
> mac <- data.frame(y,c,f)
> for (i in nrow(mac)){  mpg %>%filter(hwy <35) %>% ggplot(aes(x =
> displ, y = y[i], color = c[i])) + geom_point()
> ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in")}
>
> [[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] Bug in list.files(full.names=T)

2021-12-20 Thread Andrew Simmons
I've tried a bunch of different R versions, I can't seem to replicate what
you described. Here's the code I used:


R.pattern <- paste0("^R-", .standard_regexps()$valid_R_system_version, "$")
x <- list.files(dirname(R.home()), pattern = R.pattern, full.names = TRUE)
x <- x[file.info(x, extra_cols = FALSE)$isdir]
x <- file.path(x, "bin", "x64", "Rscript")
commands <- shQuote(x)
args <- c("--default-packages=NULL", "--vanilla",
"-e", shQuote(r"{
x <- list.files(c(
"./" ,
"C:/",
"C:/Windows/"
), full.names = TRUE)
x <- x[grepl("//", x, fixed = TRUE)]
if (length(x))
print(x)
}"))
for (command in commands) {
command <- paste(c(command, args), collapse = " ")
cat(command, "\n", sep = "")
system(command)
cat("\n")
}


which should (theoretically) open every version of R you have installed and
try the code you ran. When I ran it, I found no files with two slashes in a
row:


"C:/PROGRA~1/R/R-2.15.3/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"

"C:/PROGRA~1/R/R-3.5.3/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"

"C:/PROGRA~1/R/R-3.6.3/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"

"C:/PROGRA~1/R/R-4.0.2/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"

"C:/PROGRA~1/R/R-4.1.0/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"

"C:/PROGRA~1/R/R-4.1.1/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"

"C:/PROGRA~1/R/R-4.1.2/bin/x64/Rscript" --default-packages=NULL --vanilla
-e "
x <- list.files(c(
\"./\" ,
\"C:/\",
\"C:/Windows/\"
), full.names = TRUE)
x <- x[grepl(\"//\", x, fixed = TRUE)]
if (length(x))
print(x)
"


I can't offer much more, sorry about that. Maybe your session information
could be helpful? utils::sessionInfo()

On Sun, Dec 19, 2021 at 6:40 AM Mario Reutter  wrote:

> Dear everybody,
>
> I'm a researcher in the field of psychology and a passionate R user. After
> having updated to the newest version, I experienced a problem with
> list.files() if the parameter full.names is set to TRUE.
> A path separator "/" is now always appended to path in the output even if
> path %>% endsWith("/"). This breaks backwards compatibility in case path
> ends with a path separator. The problem occurred somewhere between R
> version 3.6.1 (2019-07-05) and 4.1.2 (2021-11-01).
>
> Example:
> >> list.files("C:/Data/", full.names=T)
> C:/Data//file.csv
>
> Expected behavior:
> Either a path separator should never be appended in accordance with
> the documentation: "full.names
> a logical value. If TRUE, the directory path is prepended to the file names
> to give a relative file path."
> Or it could only be appended if path doesn't already end with a path
> separator.
>
> My question would now be if this warrants a bug report? And if you agree,
> could someone issue the report since I'm not a member on Bugzilla?
>
> Thank you and best regards,
> Mario Reutter
>
> [[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 

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Andrew Simmons
class() does not always return the class attribute, try something more like
attr(, "class"), you'll see what I mean

On Thu, Dec 2, 2021, 15:23 Bert Gunter  wrote:

> "This is because + dispatches on the class attribute, which a string
> like "test" has set to NULL"
> Not true.
>
> > class('test')
> [1] "character"
>
> But apparently, as Denes and Jeff said, the class must be explicitly
> set, rather than relying on its built-in/implicit type.
>
> With the above hint, I looked up what ?class had to say. It is:
>
> "Note that for objects x of an implicit (or an S4) class, when a (S3)
> generic function foo(x) is called, method dispatch may use more
> classes than are returned by class(x), e.g., for a numeric matrix, the
> foo.numeric() method may apply. The exact full character vector of the
> classes which UseMethod() uses, is available as .class2(x) since R
> version 4.0.0. (This also applies to S4 objects when S3 dispatch is
> considered, see below.)"
>
> I think this is the "official" explanation, but I find it rather opaque.
>
> Thanks to all for your Help in finding the explanation. Much appreciated.
>
> Bert
>
> On Thu, Dec 2, 2021 at 12:10 PM Andrew Simmons  wrote:
> >
> > This is because + dispatches on the class attribute, which a string like
> "test" has set to NULL, so it doesn't dispatch. You can add the class
> yourself like structure("test", class = "character") and that should work.
> >
> > I'm not sure where it's explained, but most primitive functions dispatch
> on the class attribute, which is different from UseMethod which calls
> class() if the class attribute is NULL.
> >
> > I think if you want to define something like what you have written, you
> could write a function `%+%` use that instead
> >
> > On Thu, Dec 2, 2021, 14:32 Bert Gunter  wrote:
> >>
> >> ... and probably a dumb one and almost certainly not of interest to
> >> most R users. But anyway...
> >>
> >> ?"+" says:
> >> "The unary and binary arithmetic operators are generic functions:
> >> methods can be written for them individually or via the Ops group
> >> generic function. "
> >>
> >> So:
> >> "+.character" <- function(e1, e2) paste0(e1, e2)
> >> ## but this doesn't 'work':
> >> > "a" + "b"
> >> Error in "a" + "b" : non-numeric argument to binary operator
> >>
> >> ## but explicitly invoking the method does 'work' :
> >> > "+.character"('a','b')
> >> [1] "ab"
> >>
> >> ##Note also:
> >> > methods("+")
> >> [1] +.character +.Date  +.IDate*+.POSIXt+.trellis*
> >>
> >> So what am I failing to understand?
> >> Thanks.
> >>
> >> Bert Gunter
> >>
> >> __
> >> 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] A technical question on methods for "+"

2021-12-02 Thread Andrew Simmons
This is because + dispatches on the class attribute, which a string like
"test" has set to NULL, so it doesn't dispatch. You can add the class
yourself like structure("test", class = "character") and that should work.

I'm not sure where it's explained, but most primitive functions dispatch on
the class attribute, which is different from UseMethod which calls class()
if the class attribute is NULL.

I think if you want to define something like what you have written, you
could write a function `%+%` use that instead

On Thu, Dec 2, 2021, 14:32 Bert Gunter  wrote:

> ... and probably a dumb one and almost certainly not of interest to
> most R users. But anyway...
>
> ?"+" says:
> "The unary and binary arithmetic operators are generic functions:
> methods can be written for them individually or via the Ops group
> generic function. "
>
> So:
> "+.character" <- function(e1, e2) paste0(e1, e2)
> ## but this doesn't 'work':
> > "a" + "b"
> Error in "a" + "b" : non-numeric argument to binary operator
>
> ## but explicitly invoking the method does 'work' :
> > "+.character"('a','b')
> [1] "ab"
>
> ##Note also:
> > methods("+")
> [1] +.character +.Date  +.IDate*+.POSIXt+.trellis*
>
> So what am I failing to understand?
> Thanks.
>
> Bert Gunter
>
> __
> 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] Degree symbol as axis label superscript

2021-11-30 Thread Andrew Simmons
Using the degree character seems perfectly legit to me. The reason I
suggest looking at plotmath is that I think plotmath is easier to read
(that is, if you're escaping your characters with \u), and because for
anything more complicated like set notation, big sums, integrals, you'll
need to get used to plotmath to make your labels. Either way, nice
suggestion, both work perfectly well for this situation

On Tue, Nov 30, 2021, 16:24 Bert Gunter  wrote:

> True, but unnecessary with UTF-8 encodings of unicode (subject to some
> caveats, though):
>
> plot(1:5, runif(5),xlab = "Temp (°F)", ylab = "result")
>
> should work fine, where the º ("degree") symbol was inserted by the
> symbol insertion facility on my Mac. Windows has something similar.
> See the comments in the plotmath documentation just before the
> examples for more details on this, along with some caveats about the
> need for a suitable display/plot device.
>
> More laboriously, but perhaps more informatively, one can look up
> "unicode code point for degree symbol" to find that it is decimal 176.
> Then R's intToUtf8() function converts this to the UTF-8 encoding that
> **should** display as a "degree" symbol, subject to the above
> referenced caveats.
> Hence:
> > intToUtf8(176)
> [1] "°"  ##degree character
>
> ## So, for example, you can do:
> > degrF <-paste0(intToUtf8(176),"F")
> > degrF
> [1] "°F" ("degrees F")
>
> > plot(1:5, runif(5),xlab = paste0("Temp (", degrF,")"), ylab = "result")
>
> As Andrew said, you can use plotmath to do this, too; it just isn't
> needed for simple insertion of "standard" symbols.
>
> NOTE: As I am far from an expert on all of this, I would appreciate
> clarification or correction of any errors or misstatements in the
> above.
>
> Bert Gunter
>
>
> On Tue, Nov 30, 2021 at 11:34 AM Andrew Simmons 
> wrote:
> >
> > Excuse my brevity, but take a look at ?plotmath
> >
> > It has tons of tips for making pretty labels
> >
> > On Tue, Nov 30, 2021, 14:05 Rich Shepard 
> wrote:
> >
> > > I want to present the temperature on the Y-axis label as 'Water
> Temperature
> > > (oC)' with the degree symbol as a superscript.
> > >
> > > My web search found a couple of methods; one put the entire example
> string
> > > in the axis label, the other is close, but still incorrect.
> > >
> > > Source example:
> > > #define expression with superscript
> > > x_expression <- expression(x^3 ~ variable ~ label)
> > > # The example axis label is:
> > > 'X3 variable label' (with the 3 as a superscript)
> > >
> > > My use:
> > > # Set degree symbol as superscript in plot's y axis:
> > > y_expression <- expression(^o ~ C)
> > >
> > > R's error message:
> > > Error in source("../scripts/all_temp_plots.r") :
> > >../scripts/all_temp_plots.r:10:28: unexpected '^'
> > > 9: # Set degree symbol as superscript in plot's y axis:
> > > 10: y_expression <- expression(^
> > > ^
> > >
> > > What is the proper way to display a degree symbol in a plot's axis
> label?
> > >
> > > Rich
> > >
> > > __
> > > 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.
>

[[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] Degree symbol as axis label superscript [RESOLVED]

2021-11-30 Thread Andrew Simmons
I think you have to not put the word degree in quotes, something like:


graphics::plot(
x = 1,

xlab = quote(
32 * degree
)
)


works for me, though yours seems like a good solution too

On Tue, Nov 30, 2021 at 3:31 PM Rich Shepard 
wrote:

> On Tue, 30 Nov 2021, Rich Shepard wrote:
>
> > Thanks, Andrew. I will.
>
> plotmath didn't have the solution; the use of the LaTeX ^ for a superscript
> had a character or number preceeding it. Using 'degree' prints that string
> on the axis.
>
> What does work is using the unicode for the degree symbol as prefix to
> either C or F. In my case:
> ylab('Water Temperature (\u00B0C)')
> does the job.
>
> I found this solution with the DDG search string, 'degree symbol in R plot
> axis label'. This stackexchange thread has the answer:
> <
> https://stackoverflow.com/questions/51799118/writing-the-symbol-degrees-celsius-in-axis-titles-with-r-plotly
> >
>
> Regards,
>
> Rich
>
> __
> 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] Degree symbol as axis label superscript

2021-11-30 Thread Andrew Simmons
Excuse my brevity, but take a look at ?plotmath

It has tons of tips for making pretty labels

On Tue, Nov 30, 2021, 14:05 Rich Shepard  wrote:

> I want to present the temperature on the Y-axis label as 'Water Temperature
> (oC)' with the degree symbol as a superscript.
>
> My web search found a couple of methods; one put the entire example string
> in the axis label, the other is close, but still incorrect.
>
> Source example:
> #define expression with superscript
> x_expression <- expression(x^3 ~ variable ~ label)
> # The example axis label is:
> 'X3 variable label' (with the 3 as a superscript)
>
> My use:
> # Set degree symbol as superscript in plot's y axis:
> y_expression <- expression(^o ~ C)
>
> R's error message:
> Error in source("../scripts/all_temp_plots.r") :
>../scripts/all_temp_plots.r:10:28: unexpected '^'
> 9: # Set degree symbol as superscript in plot's y axis:
> 10: y_expression <- expression(^
> ^
>
> What is the proper way to display a degree symbol in a plot's axis label?
>
> Rich
>
> __
> 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] Is 'temp' a reserved/key word in R?

2021-11-30 Thread Andrew Simmons
It seems like the headers are misnamed, that should be a comma between
sampdate and param, not a period

On Tue, Nov 30, 2021, 09:35 Rich Shepard  wrote:

> A short data file:
> site_nbr,sampdate.param,quant,unit
> 31731,2005-07-12,temp,19.7,oC
> 31731,2007-03-28,temp,9,oC
> 31731,2007-06-27,temp,18.3,oC
> 31731,2007-09-26,temp,15.8,oC
> 31731,2008-01-17,temp,5.4,oC
> 31731,2008-03-27,temp,7.4,oC
> 31731,2010-04-05,temp,8.1,oC
> 31731,2010-07-26,temp,20.5,oC
> 31731,2010-10-18,temp,12.5,oC
> 31731,2011-01-10,temp,5.4,oC
>
> The import function:
> sal_temp <- read_csv("../data/geochem/sal-temp.csv", col_names = TRUE,
>   col_types = list (
>   site_nbr = col_character(),
>   sampdate = col_date(),
>   param = col_character(),
>   quant = col_double(),
>   unit = col_character()
>   ))
>
> The response from R:
> source('../scripts/test.r')
> Warning message:
> The following named parsers don't match the column names: sampdate, param
>
> I searched the web to see if 'temp' is causing the problem and didn't find
> an answer; my search term might have been flawed.
>
> If that string is a problem please suggest an alternative.
>
> TIA,
>
> Rich
>
> __
> 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] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Andrew Simmons
The as.Date function for a character class argument will try reading in two
formats (%Y-%m-%d and %Y/%m/%d).


This does not look like the format you have provided, which is why it
doesn't work. Try something like:


x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
as.Date(x, format = "%d/%m/%Y")


which produces this output:


> x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
"05/03/2017")
> as.Date(x, format = "%d/%m/%Y")
[1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
>


much better than before! I hope this helps

On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:

> Thanks Eric & Jeff.
>
> I'll certainly read up on lubridate, and the posting guide (again)
> (this should be in plain text).
>
> CSV extract below...
>
> Philip
>
> Buffer28/10/201619/11/201631/12/201616/01/2017
> 05/03/2017
> 1002.437110889-8.696748953.2392998162.443183304
> 2.346743827
> 2002.524329899-7.6888620683.3868117342.680347706
> 2.253885237
> 3002.100784256-8.0598558353.1437865072.615152896
> 2.015645973
> 4001.985608385-10.67072062.8945727912.591925038
> 2.057913137
> 5001.824982163-9.1225197362.5603507272.372226799
> 1.995863839
>
>
> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
> >
> > Hello,
> >
> > Simple but infuriating problem.
> >
> > Reading in CSV of data using :
> >
> > ```
> > # CSV file has column headers with date of scene capture in format
> dd/mm/
> > # check.names = FALSE averts R incorrectly processing dates due to '/'
> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> FALSE)
> >
> > # Converts data table from wide (many columns) to long (many rows) and
> creates the new object 'data_long'
> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain
> monthly data covering 2 years (the header row being the date, and rows 2-21
> being a value for each buffer).
> > # Column headers for columns 2:25 are mutated into a column called
> 'Date', values for each buffer and each date into the column 'LST'
> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> values_to = "LST")
> >
> > # Instructs R to treat the 'Date' column data as a date
> > data_long$Date <- as.Date(data_long$Date)
> > ```
> >
> > Using str(data), I can see that R has correctly read the dates in the
> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
> >
> > Once changing the type to 'Date', however, the date is reconfigured.
> For instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
> >
> > I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> persists or I get ```NA```.
> >
> > How do I make R change Date from 'chr' to 'date' without it going wrong?
> >
> > Suggestions/hints/solutions would be most welcome.  :)
> >
> > Thanks for your time,
> >
> > Philip
> >
> > Part-time PhD Student (Environmental Science)
> > Lancaster University, UK.
> >
> > ~
> >
> > I asked a question a few weeks ago and put together the answer I needed
> from the responses but didn't know how to say thanks on this list.  So,
> thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>
> __
> 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] Dispatching on 2 arguments?

2021-11-07 Thread Andrew Simmons
If you want to use the 'methods' package, you could do something like:


replace <- function (p1, p2, ...)
{
stop(gettextf("cannot 'replace' with arguments of class %s and %s",
sQuote(class(p1)[1L]), sQuote(class(p2)[1L])))
}


methods::setGeneric("replace")


methods::setMethod("replace", signature = c(p1 = "pm", p2 = "numeric"),
definition = replace.pm.numeric)
methods::setMethod("replace", signature = c(p1 = "pm", p2 = "character"),
definition = replace.pm.character)


First, we make the default function. I'm not sure what you want that to
look like, but I've made an example above.
Second, you make it generic so that you can provide methods for it.
Last, you add the methods you want. You specify the name of the generic
function, "replace", the classes of arguments it will accept, and then its
function definition.
You can use ?methods::setGeneric and ?methods::setMethod for help with
either of these functions. I hope this helps!

On Sun, Nov 7, 2021 at 1:55 AM Leonard Mada via R-help 
wrote:

> Dear List-members,
>
>
> I would like to experiment with dispatching on 2 arguments and have a
> few questions.
>
>
> p1 = data.frame(x=1:3, coeff=1)
> class(p1) = c("pm", class(p1));
>
> I want to replace variables in a polynomial with either:
> another polynomial, or another variable (character) or with a specific
> value.
>
>
> 1.) Can I dispatch on 2 arguments?
>
>
> replace.pm.? = function(p1, p2, ...) {...}
> or classic:
> replace.pm = function(p1, p2, ...) {
>  if(is.numeric(p2) || is.complex(p2)) return(replace.pm.numeric(p1,
> p2, ...));
>  if(is.character(p2)) return(replace.pm.character(p1, p2=p2, ...));
> else ...
>
> }
>
>
> I will provide some realistic examples below.
>
>
> 2.) Advantages / Disadvantages of each method
> What are the advantages or disadvantages to each of these methods?
> I do not yet understand what should be the best design.
>
>
> Real example:
>
>
> ### Quintic
> p1 = toPoly.pm("x^5 - 5*K*x^3 - 5*(K^2 + K)*x^2 - 5*K^3*x - K^4 - 6*K^3
> + 5*K^2 - K")
> # fractional powers: [works as well]
> r = toPoly.pm("K^(4/5) + K^(3/5) + K^(1/5)")
> # - we just found a root of a non-trivial quintic!
> #   all variables/monomials got cancelled;
> replace.pm(p1, r, "x", pow=1)
>
> # more formal
> r = toPoly.pm("k^4*m^4 + k^3*m^3 + k*m")
> # m^5 = 1; # m = any of the 5 roots of unity of order 5;
> pR = p1;
> pR = replace.pm(pR, r, xn="x") # poly
> pR = replace.pm(pR, "K", xn="k", pow=5) # character
> pR = replace.pm(pR, 1, xn="m", pow=5) # value
> pR # the roots worked! [no remaining rows]
> # - we just found ALL 5 roots!
>
>
> The code is on Github (see below).
>
>
> Sincerely,
>
>
> Leonard
>
> =
>
> # very experimental code
> # some names & arguments may change;
>
> source("Polynomials.Helper.R")
> # also required, but are loaded automatically if present in wd;
> # source("Polynomials.Helper.Parser.R")
> # source("Polynomials.Helper.Format.R")
> ### not necessary for this Test (just loaded)
> # source("Polynomials.Helper.D.R")
> # source("Polynomials.Helper.Factorize.R")
> # the libraries pracma & polynom are not really required for this test
> either;
>
> ### Github:
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.R
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Parser.R
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Format.R
> # not necessary for this Test
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.D.R
>
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Factorize.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] names.data.frame?

2021-11-03 Thread Andrew Simmons
First, your signature for names.pm is wrong. It should look something more
like:


names.pm <- function (x)
{
}


As for the body of the function, you might do something like:


names.pm <- function (x)
{
NextMethod()
}


but you don't need to define a names method if you're just going to call
the next method. I would suggest not defining a names method at all.


As a side note, I would suggest making your class through the methods
package, with methods::setClass("pm", ...)
See the documentation for setClass for more details, it's the recommended
way to define classes in R.

On Wed, Nov 3, 2021 at 2:36 PM Leonard Mada via R-help 
wrote:

> Dear List members,
>
>
> Is there a way to access the default names() function?
>
>
> I tried the following:
>
> # Multi-variable polynomial
>
> p = data.frame(x=1:3, coeff=1)
>
> class(p) = c("pm", class(p));
>
>
> names.pm = function(p) {
> # .Primitive("names")(p) # does NOT function
> # .Internal("names")(p) # does NOT function
> # nms = names.default(p) # does NOT exist
> # nms = names.data.frame(p) # does NOT exist
> # nms = names(p); # obvious infinite recursion;
> nms = names(unclass(p));
> }
>
>
> Alternatively:
>
> Would it be better to use dimnames.pm instead of names.pm?
>
> I am not fully aware of the advantages and disadvantages of dimnames vs
> names.
>
>
> Sincerely,
>
>
> Leonard
>
> __
> 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] sink() not working as expected

2021-11-02 Thread Andrew Simmons
cat in R behaves similarly to cat in unix-alikes, sends text to a stdout.
Usually, that stdout would be a file, but usually in R it is the R Console.
I think it might also help to note the difference between cat and print:


x <- "test\n"
cat(x)
print(x)


produces


> cat(x)
test
> print(x)
[1] "test\n"


With that said, wrap each expressions in cat or print depending on which
you want. Also, I would probably add a tryCatch with argument finally as {
sink()
}
which will guarantee the file connection is severed, even if an error is
raised. Hope this helps

On Tue, Nov 2, 2021, 15:52 Rich Shepard  wrote:

> On Tue, 2 Nov 2021, Bert Gunter wrote:
>
> > What do you think these 2 lines are doing?
> > cat ('corvalis discharge summary\n')
> > print(cat)
>
> Bert,
>
> If I used them in linux cat would display the file (as do more and less)
> and
> print() would be replaced with lpr
>
> > Please consult ?cat . You might also spend a bit of (more?) time with an
> R
> > tutorial or two as you seem confused about how assignment (<-) works. Or
> > maybe I'm confused about what is confusing you
>
> Having never before needed to use cat in R, and not seeing it explained in
> the one sink() web page, I didn't think to look at the help page.
>
> Now, using cat and not print line-by-line the first set of commands work,
> but not the second.
>
> More reading coming up.
>
> Rich
>
> __
> 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] sink() not working as expected

2021-11-02 Thread Andrew Simmons
You probably want to use cat and print for these lines. These things won't
print when not run at the top level, so if you want them to print, you must
specify that.

On Tue, Nov 2, 2021, 13:18 Rich Shepard  wrote:

> I've read ?sink and several web pages about it but it's not working
> properly
> when I have the commands in a script and source() them.
>
> The file:
> library(tidyverse)
> library(lubridate)
>
> sink('data-summaries.txt')
> 'corvalis discharge summary\n'
> summary(cor_disc)
> sd(cor_disc$cfs)
> '-\n'
> sink()
>
> > source('summary_stats.R')
> results in an empty file.
>
> When I enter them line-by-line on the R command line I get output including
> multiple lines listing the PWD and ignoring the newline:
>
> [1] "/path/to/PWD"
> [1] "corvalis discharge summary\n"
> [1] "/path/to/PWD"
> site_nbr  year   mon  day
>   Length:415263  Min.   :2009   Min.   : 1.000   Min.   : 1.00
>   Class :character   1st Qu.:2012   1st Qu.: 4.000   1st Qu.: 8.00
>   Mode  :character   Median :2015   Median : 7.000   Median :16.00
>  Mean   :2015   Mean   : 6.552   Mean   :15.74
>  3rd Qu.:2018   3rd Qu.:10.000   3rd Qu.:23.00
>  Max.   :2021   Max.   :12.000   Max.   :31.00
> hr min tz cfs
>   Min.   : 0.00   Min.   : 0.00   Length:415263  Min.   :1000
>   1st Qu.: 5.00   1st Qu.: 0.00   Class :character   1st Qu.:1950
>   Median :11.00   Median :15.00   Mode  :character   Median :4740
>   Mean   :11.49   Mean   :22.44  Mean   :4624
>   3rd Qu.:17.00   3rd Qu.:30.00  3rd Qu.:6580
>   Max.   :23.00   Max.   :55.00  Max.   :9990
> [1] "/path/to/PWD"
> [1] 2600.546
> [1] "/path/to/PWD"
> [1] "-\n"
> [1] "/path/to/PWD"
> ~
>
> What am I doing incorrectly here? I've looked at examples on web pages
> without learning where I'm erring.
>
> TIA,
>
> Rich
>
> __
> 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] Is there a hash data structure for R

2021-11-02 Thread Andrew Simmons
If you're thinking about using environments, I would suggest you initialize
them like


x <- new.env(parent = emptyenv())


Since environments have parent environments, it means that requesting a
value from that environment can actually return the value stored in a
parent environment (this isn't an issue for [[ or $, this is exclusively an
issue with assign, get, and exists)
Or, if you've already got your values stored in a list that you want to
turn into an environment:


x <- list2env(listOfValues, parent = emptyenv())


Hope this helps!


On Tue, Nov 2, 2021, 06:49 Yonghua Peng  wrote:

> But for data.frame the colnames can be duplicated. Am I right?
>
> Regards.
>
> On Tue, Nov 2, 2021 at 6:29 PM Jan van der Laan  wrote:
>
> >
> > True, but in a lot of cases where a python user might use a dict an R
> > user will probably use a list; or when we are talking about arrays of
> > dicts in python, the R solution will probably be a data.frame (with each
> > dict field in a separate column).
> >
> > Jan
> >
> >
> >
> >
> > On 02-11-2021 11:18, Eric Berger wrote:
> > > One choice is
> > > new.env(hash=TRUE)
> > > in the base package
> > >
> > >
> > >
> > > On Tue, Nov 2, 2021 at 11:48 AM Yonghua Peng  wrote:
> > >
> > >> I know this is a newbie question. But how do I implement the hash
> > structure
> > >> which is available in other languages (in python it's dict)?
> > >>
> > >> I know there is the list, but list's names can be duplicated here.
> > >>
> > >>> x <- list(x=1:5,y=month.name,x=3:7)
> > >>
> > >>> x
> > >>
> > >> $x
> > >>
> > >> [1] 1 2 3 4 5
> > >>
> > >>
> > >> $y
> > >>
> > >>   [1] "January"   "February"  "March" "April" "May"
>  "June"
> > >>
> > >>   [7] "July"  "August""September" "October"   "November"
> > "December"
> > >>
> > >>
> > >> $x
> > >>
> > >> [1] 3 4 5 6 7
> > >>
> > >>
> > >>
> > >> Thanks a lot.
> > >>
> > >>  [[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.
> > >
> >
> > __
> > 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.
>

[[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] by group

2021-11-01 Thread Andrew Simmons
I would usually use 'tapply'. It splits an object into groups, performs
some function on each group, and then (optionally) converts the input to
something simpler.
For example:


tapply(dat$wt, dat$Year, mean)  # mean by Year
tapply(dat$wt, dat$Sex , mean)  # mean by Sex
tapply(dat$wt, list(dat$Year, dat$Sex), mean)  # mean by Year and Sex


The documentation ?tapply has many more details about how this works, but
that's the basics at least. I hope this helps!

On Mon, Nov 1, 2021 at 5:09 PM Val  wrote:

> Hi All,
>
> How can I generate mean by group. The sample data looks like as follow,
> dat<-read.table(text="Year Sex wt
> 2001 M 15
> 2001 M 14
> 2001 M 16
> 2001 F 12
> 2001 F 11
> 2001 F 13
> 2002 M 14
> 2002 M 18
> 2002 M 17
> 2002 F 11
> 2002 F 15
> 2002 F 14
> 2003 M 18
> 2003 M 13
> 2003 M 14
> 2003 F 15
> 2003 F 10
> 2003 F 11  ",header=TRUE)
>
> The desired  output  is,
>  MF
> 20011512
> 200216.33   13.33
> 200315  12
>
> 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.
>

[[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] generate random numeric

2021-10-29 Thread Andrew Simmons
It might not be random, depending upon a seed being used (usually by
set.seed or RNGkind).

However, it's the best method for generating a random number within a
specified range without weights.

If you want weights, there are many other random number generation
functions, most notably rnorm. You can find a lot more in the stats
package.


On Fri, Oct 29, 2021, 02:39 Ken Peng  wrote:

> I saw runif(1) can generate a random num, is this the true random?
>
> > runif(1)
> [1] 0.8945383
>
> What's the other better method?
>
> Thank you.
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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] here Package

2021-10-17 Thread Andrew Simmons
You've just got the brackets in the wrong spot:


creditsub <- read.csv(unz(here::here("Data.zip"), "creditcardsub.csv"))

instead of

creditsub <- read.csv(unz(here::here("Data.zip", "creditcardsub.csv")))


Also, you probably want to save that connection and close it manually.


creditsub <- read.csv(con <- unz(here::here("Data.zip"),
"creditcardsub.csv")) ; close(con)


I hope this helps!

On Sun, Oct 17, 2021 at 4:23 PM Jeff Reichman 
wrote:

> R-help
>
>
>
> I have a R project that contains an R Notebook and I am trying to use the
> "here" function  to access a file.  Before using R projects file I would
> set
> the working dir in a R setup chunk and run the following command (for
> example)
>
>
>
> creditsub <- read.csv(unz(Data.zip", "creditcardsub.csv"))
>
>
>
> This works just fine but I'm trying to see if I can use the here function
> now but no luck. I sort of assumed it would be something like
>
>
>
> creditsub <- read.csv(unz(here::here(Data.zip", "creditcardsub.csv"))) -
> nope. I've also tried placing it in other locations  still no luck so I'm
> wondering if I can even use it in this application
>
>
>
> Jeff
>
>
>
>
> [[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] How to select given row of conditionally subsetted dataframe?

2021-10-14 Thread Andrew Simmons
You're missing a comma, this should fix it

df[(df$Y>0.2) & (df$X<10),][2, ]

On Thu, Oct 14, 2021, 03:51 Luigi Marongiu  wrote:

> Hello,
> I have selected a subset of a dataframe with the vector syntax (if
> this is the name):
> ```
> > df[(df$Y>0.2) & (df$X<10),]
>  YX
> 10 0.2200642 1.591589
> 13 0.2941828 1.485951
> ```
> How can I select only the second row? I only managed to get the whole
> of whole columns:
> ```
> > df[(df$Y>0.2) & (df$X<10),2]
> [1] 1.591589 1.485951
> > df[(df$Y>0.2) & (df$X<10),][2]
>  X
> 10 1.591589
> 13 1.485951
> > df[(df$Y>0.2) & (df$X<10),][1]
>  Y
> 10 0.2200642
> 13 0.2941828
> ```
> 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.
>

[[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] unexpected behavior in apply

2021-10-08 Thread Andrew Simmons
Hello,


The issue comes that 'apply' tries to coerce its argument to a matrix. This
means that all your columns will become character class, and the result
will not be what you wanted. I would suggest something more like:


sapply(d, function(x) all(x[!is.na(x)] <= 3))

or

vapply(d, function(x) all(x[!is.na(x)] <= 3), NA)


Also, here is a different method that might look cleaner:


sapply(d, function(x) all(x <= 3, na.rm = TRUE))

vapply(d, function(x) all(x <= 3, na.rm = TRUE), NA)


It's up to you which you choose. I hope this helps!

On Fri, Oct 8, 2021 at 1:50 PM Derickson, Ryan, VHA NCOD via R-help <
r-help@r-project.org> wrote:

> Hello,
>
> I'm seeing unexpected behavior when using apply() compared to a for loop
> when a character vector is part of the data subjected to the apply
> statement. Below, I check whether all non-missing values are <= 3. If I
> include a character column, apply incorrectly returns TRUE for d3. If I
> only pass the numeric columns to apply, it is correct for d3. If I use a
> for loop, it is correct.
>
> > d<-data.frame(d1 = letters[1:3],
> +   d2 = c(1,2,3),
> +   d3 = c(NA,NA,6))
> >
> > d
>   d1 d2 d3
> 1  a  1 NA
> 2  b  2 NA
> 3  c  3  6
> >
> > # results are incorrect
> > apply(d, 2, FUN=function(x)all(x[!is.na(x)] <= 3))
>d1d2d3
> FALSE  TRUE  TRUE
> >
> > # results are correct
> > apply(d[,2:3], 2, FUN=function(x)all(x[!is.na(x)] <= 3))
>d2d3
>  TRUE FALSE
> >
> > # results are correct
> > for(i in names(d)){
> +   print(all(d[!is.na(d[,i]),i] <= 3))
> + }
> [1] FALSE
> [1] TRUE
> [1] FALSE
>
>
> Finally, if I remove the NA values from d3 and include the character
> column in apply, it is correct.
>
> > d<-data.frame(d1 = letters[1:3],
> +   d2 = c(1,2,3),
> +   d3 = c(4,5,6))
> >
> > d
>   d1 d2 d3
> 1  a  1  4
> 2  b  2  5
> 3  c  3  6
> >
> > # results are correct
> > apply(d, 2, FUN=function(x)all(x[!is.na(x)] <= 3))
>d1d2d3
> FALSE  TRUE FALSE
>
>
> Can someone help me understand what's happening?
>
> __
> 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] Rename variables starting with digits

2021-10-05 Thread Andrew Simmons
Hello,


I think you have to use 'matches' instead of 'starts_with', I believe
starts_with does not accept a regular expression whereas matches does.

On Tue, Oct 5, 2021 at 12:15 PM Anne Zach  wrote:

> Dear R users,
>
> I have a dataframe that contains several variables, among which 105
> correspond to scores on certain trials. Unfortunately, when I imported this
> dataframe into R, I realised that the variable names corresponding to each
> trial begin with digits, which violates R naming conventions.
>
> I am trying to relabel these variables by adding a 'v' as a prefix to each
> of them, I'd like to use tidyverse, but I am struggling with this process
> of renaming. When I run this chunk of code, no error occurs but my
> variables are not renamed. I'm fairly new to R and I can't understand what
> I'm doing wrong.
>
> ```{r}
>
> behavioral_df <- behavioral_df %>% rename_with(.fn =  ~paste0("v"),
> starts_with('^\\d'))
>
> ```
>
> I appreciate if you can help.
>
> Best,
> Anne
>
> [[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] Word-Wrapper Library/Package?

2021-09-28 Thread Andrew Simmons
'strwrap' should wrap at the target column, so I think it's behaving
correctly. You could do + 1 if you're expecting it to wrap immediately
after the target column.

As far as splitting while trying to minimize a penalty, I don't think
strwrap can do that, and I don't know of any packages that do such a thing.
If such a thing exists in another language, there's probably an R package
with a similar name containing ports of such functions, that might be your
best bet. I hope this helps.

On Tue, Sep 28, 2021, 23:51 Leonard Mada  wrote:

> Thank you Andrew.
>
>
> I will explore this function more, although I am struggling to get it to
> work properly:
>
> strwrap("Abc. B. Defg", 7)
> # [1] "Abc." "B."   "Defg"
>
> # both "Abc. B." and "B. Defg" are 7 characters long.
>
> strwrap(paste0(rep("ab", 7), collapse=""), 7)
> # [1] "ababababababab"
>
>
> Can I set an absolute maximum width?
>
> It would be nice to have an algorithm that computes a penalty for the
> split and selects the split with the smallest penalty (when no obvious
> split is possible).
>
>
> Sincerely,
>
>
> Leonard
>
>
>
> On 9/29/2021 6:30 AM, Andrew Simmons wrote:
>
> I think what you're looking for is 'strwrap', it's in package base.
>
> On Tue, Sep 28, 2021, 22:26 Leonard Mada via R-help 
> wrote:
>
>> Dear R-Users,
>>
>>
>> Does anyone know any package or library that implements functions for
>> word wrapping?
>>
>>
>> I did implement a very rudimentary one (Github link below), but would
>> like to avoid to reinvent the wheel. Considering that word-wrapping is a
>> very common task, it should be available even in base R (e.g. in a
>> "format" module/package).
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>> ===
>>
>> The latest versions of the functions are on Github:
>>
>> https://github.com/discoleo/R/blob/master/Stat/Tools.CRAN.R
>> # Note:
>> # - the function implementing word wrapping: split.N.line(...);
>> # - for the example below: the functions defined in Tools.CRAN.R are
>> required;
>>
>>
>> Examples:
>> ### Search CRAN
>> library(pkgsearch)
>>
>> searchCran = function(s, from=1, len=60, len.print=20, extend="*",
>>  sep=" ", sep.h="-") {
>>  if( ! is.null(extend)) s = paste0(s, extend);
>>  x = advanced_search(s, size=len, from=from);
>>  if(length(x$package_data) == 0) {
>>  cat("No packages found!", sep="\n");
>>  } else {
>>  scroll.pkg(x, len=len.print, sep=sep, sep.h=sep.h);
>>  }
>>  invisible(x)
>> }
>>
>> # with nice formatting & printing:
>> x = searchCran("text", from=60, sep.h="-")
>>
>> scroll.pkg(x, start=20, len=21, sep.h = "-*")
>> # test of sep.h=NULL vs ...
>>
>>
>> Notes:
>>
>> 1.) split.N.line:
>>
>> - was implemented to output a pre-specified number of lines (kind of
>> "maxLines"), but this is not required from an actual word-wrapper;
>>
>> - it was an initial design decision when implementing the format.lines()
>> function; but I plan to implement a 1-pass exact algorithm during the
>> next few days anyway;
>>
>> 2.) Refactoring
>>
>> - I will also move the formatting code to a new file: probably
>> Tools.Formatting.R;
>>
>> - the same applies for the formatting code for ftable (currently in file
>> Tools.Data.R);
>>
>> 3.) Package gridtext
>>
>> - seems to have some word-wrapping functionality, but does not seem to
>> expose it;
>>
>> - I am also currently focused on character-based word wrapping (e.g. for
>> RConsole);
>>
>>
>>
>> [[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] Word-Wrapper Library/Package?

2021-09-28 Thread Andrew Simmons
I think what you're looking for is 'strwrap', it's in package base.

On Tue, Sep 28, 2021, 22:26 Leonard Mada via R-help 
wrote:

> Dear R-Users,
>
>
> Does anyone know any package or library that implements functions for
> word wrapping?
>
>
> I did implement a very rudimentary one (Github link below), but would
> like to avoid to reinvent the wheel. Considering that word-wrapping is a
> very common task, it should be available even in base R (e.g. in a
> "format" module/package).
>
>
> Sincerely,
>
>
> Leonard
>
> ===
>
> The latest versions of the functions are on Github:
>
> https://github.com/discoleo/R/blob/master/Stat/Tools.CRAN.R
> # Note:
> # - the function implementing word wrapping: split.N.line(...);
> # - for the example below: the functions defined in Tools.CRAN.R are
> required;
>
>
> Examples:
> ### Search CRAN
> library(pkgsearch)
>
> searchCran = function(s, from=1, len=60, len.print=20, extend="*",
>  sep=" ", sep.h="-") {
>  if( ! is.null(extend)) s = paste0(s, extend);
>  x = advanced_search(s, size=len, from=from);
>  if(length(x$package_data) == 0) {
>  cat("No packages found!", sep="\n");
>  } else {
>  scroll.pkg(x, len=len.print, sep=sep, sep.h=sep.h);
>  }
>  invisible(x)
> }
>
> # with nice formatting & printing:
> x = searchCran("text", from=60, sep.h="-")
>
> scroll.pkg(x, start=20, len=21, sep.h = "-*")
> # test of sep.h=NULL vs ...
>
>
> Notes:
>
> 1.) split.N.line:
>
> - was implemented to output a pre-specified number of lines (kind of
> "maxLines"), but this is not required from an actual word-wrapper;
>
> - it was an initial design decision when implementing the format.lines()
> function; but I plan to implement a 1-pass exact algorithm during the
> next few days anyway;
>
> 2.) Refactoring
>
> - I will also move the formatting code to a new file: probably
> Tools.Formatting.R;
>
> - the same applies for the formatting code for ftable (currently in file
> Tools.Data.R);
>
> 3.) Package gridtext
>
> - seems to have some word-wrapping functionality, but does not seem to
> expose it;
>
> - I am also currently focused on character-based word wrapping (e.g. for
> RConsole);
>
>
>
> [[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] Improvement: function cut

2021-09-17 Thread Andrew Simmons
I disagree, I don't really think it's too long or ugly, but if you think it
is, you could abbreviate it as 'i'.


x <- 0:20
breaks1 <- seq.int(0, 16, 4)
breaks2 <- seq.int(0, 20, 4)
data.frame(
cut(x, breaks1, right = FALSE, i = TRUE),
cut(x, breaks2, right = FALSE, i = TRUE),
check.names = FALSE
)


I hope this helps.

On Fri, Sep 17, 2021 at 6:26 PM Leonard Mada  wrote:

> Hello Andrew,
>
>
> But "cut" generates factors. In most cases with real data one expects to
> have also the ends of the interval: the argument "include.lowest" is both
> ugly and too long.
>
> [The test-code on the ftable thread contains this error! I have run
> through this error a couple of times.]
>
>
> The only real situation that I can imagine to be problematic:
>
> - if the interval goes to +Inf (or -Inf): I do not know if there would be
> any effects when including +Inf (or -Inf).
>
>
> Leonard
>
>
> On 9/18/2021 1:14 AM, Andrew Simmons wrote:
>
> While it is not explicitly mentioned anywhere in the documentation for
> .bincode, I suspect 'include.lowest = FALSE' is the default to keep the
> definitions of the bins consistent. For example:
>
>
> x <- 0:20
> breaks1 <- seq.int(0, 16, 4)
> breaks2 <- seq.int(0, 20, 4)
> cbind(
> .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
> .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
> )
>
>
> by having 'include.lowest = TRUE' with different ends, you can get
> inconsistent behaviour. While this probably wouldn't be an issue with
> 'real' data, this would seem like something you'd want to avoid by default.
> The definitions of the bins are
>
>
> [0, 4)
> [4, 8)
> [8, 12)
> [12, 16]
>
>
> and
>
>
> [0, 4)
> [4, 8)
> [8, 12)
> [12, 16)
> [16, 20]
>
>
> so you can see where the inconsistent behaviour comes from. You might be
> able to get R-core to add argument 'warn', but probably not to change the
> default of 'include.lowest'. I hope this helps
>
>
> On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada  wrote:
>
>> Thank you Andrew.
>>
>>
>> Is there any reason not to make: include.lowest = TRUE the default?
>>
>>
>> Regarding the NA:
>>
>> The user still has to suspect that some values were not included and run
>> that test.
>>
>>
>> Leonard
>>
>>
>> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>>
>> Regarding your first point, argument 'include.lowest' already handles
>> this specific case, see ?.bincode
>>
>> Your second point, maybe it could be helpful, but since both
>> 'cut.default' and '.bincode' return NA if a value isn't within a bin, you
>> could make something like this on your own.
>> Might be worth pitching to R-bugs on the wishlist.
>>
>>
>>
>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
>> wrote:
>>
>>> Hello List members,
>>>
>>>
>>> the following improvements would be useful for function cut (and
>>> .bincode):
>>>
>>>
>>> 1.) Argument: Include extremes
>>> extremes = TRUE
>>> if(right == FALSE) {
>>> # include also right for last interval;
>>> } else {
>>> # include also left for first interval;
>>> }
>>>
>>>
>>> 2.) Argument: warn = TRUE
>>>
>>> Warn if any values are not included in the intervals.
>>>
>>>
>>> Motivation:
>>> - reduce risk of errors when using function cut();
>>>
>>>
>>> Sincerely,
>>>
>>>
>>> Leonard
>>>
>>> __
>>> 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] Improvement: function cut

2021-09-17 Thread Andrew Simmons
While it is not explicitly mentioned anywhere in the documentation for
.bincode, I suspect 'include.lowest = FALSE' is the default to keep the
definitions of the bins consistent. For example:


x <- 0:20
breaks1 <- seq.int(0, 16, 4)
breaks2 <- seq.int(0, 20, 4)
cbind(
.bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
.bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
)


by having 'include.lowest = TRUE' with different ends, you can get
inconsistent behaviour. While this probably wouldn't be an issue with
'real' data, this would seem like something you'd want to avoid by default.
The definitions of the bins are


[0, 4)
[4, 8)
[8, 12)
[12, 16]


and


[0, 4)
[4, 8)
[8, 12)
[12, 16)
[16, 20]


so you can see where the inconsistent behaviour comes from. You might be
able to get R-core to add argument 'warn', but probably not to change the
default of 'include.lowest'. I hope this helps


On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada  wrote:

> Thank you Andrew.
>
>
> Is there any reason not to make: include.lowest = TRUE the default?
>
>
> Regarding the NA:
>
> The user still has to suspect that some values were not included and run
> that test.
>
>
> Leonard
>
>
> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>
> Regarding your first point, argument 'include.lowest' already handles this
> specific case, see ?.bincode
>
> Your second point, maybe it could be helpful, but since both 'cut.default'
> and '.bincode' return NA if a value isn't within a bin, you could make
> something like this on your own.
> Might be worth pitching to R-bugs on the wishlist.
>
>
>
> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
> wrote:
>
>> Hello List members,
>>
>>
>> the following improvements would be useful for function cut (and
>> .bincode):
>>
>>
>> 1.) Argument: Include extremes
>> extremes = TRUE
>> if(right == FALSE) {
>> # include also right for last interval;
>> } else {
>> # include also left for first interval;
>> }
>>
>>
>> 2.) Argument: warn = TRUE
>>
>> Warn if any values are not included in the intervals.
>>
>>
>> Motivation:
>> - reduce risk of errors when using function cut();
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>> __
>> 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] Improvement: function cut

2021-09-17 Thread Andrew Simmons
Regarding your first point, argument 'include.lowest' already handles this
specific case, see ?.bincode

Your second point, maybe it could be helpful, but since both 'cut.default'
and '.bincode' return NA if a value isn't within a bin, you could make
something like this on your own.
Might be worth pitching to R-bugs on the wishlist.



On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
wrote:

> Hello List members,
>
>
> the following improvements would be useful for function cut (and .bincode):
>
>
> 1.) Argument: Include extremes
> extremes = TRUE
> if(right == FALSE) {
> # include also right for last interval;
> } else {
> # include also left for first interval;
> }
>
>
> 2.) Argument: warn = TRUE
>
> Warn if any values are not included in the intervals.
>
>
> Motivation:
> - reduce risk of errors when using function cut();
>
>
> Sincerely,
>
>
> Leonard
>
> __
> 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] Evaluating lazily 'f<-' ?

2021-09-16 Thread Andrew Simmons
In your case, yes, there is a negative impact to formatting the code like:

x <- `padding<-`(right(x), ...)

and it comes from using 'substitute' without specifying an environment / /
list. It's my biggest problem with the tidyverse packages, the use of
non-standard evaluation. 'substitute' was originally created for simple
things like getting informative labels for data sets and plots, as well as
for 'delayedAssign'. For example, you could write your function to use the
above standard syntax, something like:

`padding<-` <- function (x, ..., value)
{
sx <- substitute(x)
choices <- c("bottom", "left", "top", "right")
if (!is.call(sx) || !is.symbol(sx[[1L]]) ||
!(k <- match(as.character(sx[[1L]]), choices, nomatch = 0L)))
stop(gettextf("invalid 'x', must be a call to %s",
paste(sQuote(choices), collapse = ", ")))
choice <- choices[[k]]
if (length(sx) != 2L)
stop(gettextf("invalid 'x', %d arguments passed to '%s' which
requires 1",
length(sx) - 1L, choice))
x <- eval(sx[[2L]], parent.frame())
# do whatever else with x, choice, ..., value
}

but given that you cannot use it like

padding(right(x)) <- 5

I can't say that I recommend it. Additionally, even if you define
`padding<-` like this, because of the non-standard evaluation from
'substitute', it means you CAN'T make a wrapper function for `padding<-`:

wrapper <- function (x, ..., value)
{
# not sure exactly what this wrapper should do,
# we'll add another element to 'value'
`padding<-`(x = x, ..., value = c(list(value), "test"))
}

this won't work for a case like:

wrapper(right(letters))

because now, when you do 'substitute', it will give you the literal symbol
'x', not really what you wanted, because of the non-standard evaluation. Of
course, do whatever you want to make the syntax look the way you want, but
I think you're going about this the wrong way. I think the 'which' argument
I previously suggested will serve you far better. I hope this helps.

On Wed, Sep 15, 2021 at 2:26 AM Leonard Mada  wrote:

> Hello Andrew,
>
>
> On 9/15/2021 6:53 AM, Andrew Simmons wrote:
>
> names(x) <- c("some names")
>
> if different from
>
> `names<-`(x, value = c("some names"))
>
> because the second piece of code does not ever call `<-`. The first piece
> of code is (approximately) equivalent to
>
> `*tmp*` <- x
> `*tmp*` <- `names<-`(`*tmp*`, value = c("some names"))
> x <- `*tmp*`
>
>
> This is my question:
>
> Would there be any negative impact if the code is changed to:
>
> x <- 'names<-'(x, value=...);
>
> The "x" will be evaluated inside 'names<-' and this function outputs &
> assigns "x". The "creation" of "x" in the current environment is done only
> after the call to 'names<-' (if it did not exist before).
>
>
> Leonard
>
>
>
> Another example,
>
> y <- `names<-`(x, value = c("some names"))
>
> now y will be equivalent to x if we did
>
> names(x) <- c("some names")
>
> except that the first will not update x, it will still have its old names.
>
> On Mon, Sep 13, 2021 at 4:33 PM Leonard Mada  wrote:
>
>>
>> On 9/13/2021 11:28 PM, Andrew Simmons wrote:
>>
>> In the example you gave : r(x) <- 1
>> r(x) is never evaluated, the above calls `r<-`,
>> in fact r does not even have to be an existing function.
>>
>>
>> I meant:
>>
>> '*tmp*' <- x; # "x" is evaluated here;
>>
>> 'r<-' is called after this step, which makes sense in the case of
>> subsetting;
>>
>>
>> But I am wondering if changing this behaviour, when NO subsetting is
>> performed, would have any impact.
>>
>> e.g. names(x) = c("some names");
>>
>> # would it have any impact to skip the evaluation of "x" and call
>> directly:
>>
>> 'names<-'(x, value);
>>
>>
>> Leonard
>>
>>
>>
>> On Mon, Sep 13, 2021, 16:18 Leonard Mada  wrote:
>>
>>> Hello,
>>>
>>>
>>> I have found the evaluation: it is described in the section on
>>> subsetting. The forced evaluation makes sense for subsetting.
>>>
>>>
>>> On 9/13/2021 9:42 PM, Leonard Mada wrote:
>>>
>>> Hello Andrew,
>>>
>>>
>>> I try now to understand the evaluation of the expression:
>>>
>>> e = expression(r(x) <- 1)
>>>
>>> # parameter named "value" seems to be required;
&g

Re: [R] How to remove all rows that have a numeric in the first (or any) column

2021-09-14 Thread Andrew Simmons
I'd like to point out that base R can handle a list as a data frame column,
it's just that you have to make the list of class "AsIs". So in your example

temp <- list("Hello", 1, 1.1, "bye")

data.frame(alpha = 1:4, beta = I(temp))

means that column "beta" will still be a list.



On Wed, Sep 15, 2021, 00:40 Avi Gross via R-help 
wrote:

> Calling something a data.frame does not make it a data.frame.
>
> The abbreviated object shown below is a list of singletons. If it is a
> column in a larger object that is a data.frame, then it is a list column
> which is valid but can be ticklish to handle within base R but less so in
> the tidyverse.
>
> For example, if I try to make a data.frame the normal way, the list gets
> made into multiple columns and copied to each row. Not what was expected. I
> think some tidyverse functionality does better.
>
> Like this:
>
> library(tidyverse)
> temp=list("Hello", 1, 1.1, "bye")
>
> Now making a data.frame has an odd result:
>
> > mydf=data.frame(alpha=1:4, beta=temp)
> > mydf
> alpha beta..Hello. beta.1 beta.1.1 beta..bye.
> 1 1Hello  1  1.1bye
> 2 2Hello  1  1.1bye
> 3 3Hello  1  1.1bye
> 4 4Hello  1  1.1bye
>
> But a tibble handles it:
>
> > mydf=tibble(alpha=1:4, beta=temp)
> > mydf
> # A tibble: 4 x 2
> alpha beta
>  
>   1 1 
>   2 2 
>   3 3 
>   4 4 
>
> So if the data does look like this, with a list column, but access can be
> tricky as subsetting a list with [] returns a list and you need [[]].
>
> I found a somehwhat odd solution like this:
>
> mydf %>%
>filter(!map_lgl(beta, is.numeric)) -> mydf2
> # A tibble: 2 x 2
> alpha beta
>  
>   1 1 
>   2 4 
>
> When I saved that result into mydf2, I got this.
>
> Original:
>
>   > str(mydf)
> tibble [4 x 2] (S3: tbl_df/tbl/data.frame)
> $ alpha: int [1:4] 1 2 3 4
> $ beta :List of 4
> ..$ : chr "Hello"
> ..$ : num 1
> ..$ : num 1.1
> ..$ : chr "bye"
>
> Output when any row with a numeric is removed:
>
> > str(mydf2)
> tibble [2 x 2] (S3: tbl_df/tbl/data.frame)
> $ alpha: int [1:2] 1 4
> $ beta :List of 2
> ..$ : chr "Hello"
> ..$ : chr "bye"
>
> So if you try variations on your code motivated by what I show, good luck.
> I am sure there are many better ways but I repeat, it can be tricky.
>
> -Original Message-
> From: R-help  On Behalf Of Jeff Newmiller
> Sent: Tuesday, September 14, 2021 11:54 PM
> To: Gregg Powell 
> Cc: Gregg Powell via R-help 
> Subject: Re: [R] How to remove all rows that have a numeric in the first
> (or any) column
>
> You cannot apply vectorized operators to list columns... you have to use a
> map function like sapply or purrr::map_lgl to obtain a logical vector by
> running the function once for each list element:
>
> sapply( VPN_Sheet1$HVA, is.numeric )
>
> On September 14, 2021 8:38:35 PM PDT, Gregg Powell <
> g.a.pow...@protonmail.com> wrote:
> >Here is the output:
> >
> >> str(VPN_Sheet1$HVA)
> >List of 2174
> > $ : chr "Email: f...@fff.com"
> > $ : num 1
> > $ : chr "Eloisa Libas"
> > $ : chr "Percival Esquejo"
> > $ : chr "Louchelle Singh"
> > $ : num 2
> > $ : chr "Charisse Anne Tabarno, RN"
> > $ : chr "Sol Amor Mucoy"
> > $ : chr "Josan Moira Paler"
> > $ : num 3
> > $ : chr "Anna Katrina V. Alberto"
> > $ : chr "Nenita Velarde"
> > $ : chr "Eunice Arrances"
> > $ : num 4
> > $ : chr "Catherine Henson"
> > $ : chr "Maria Carla Daya"
> > $ : chr "Renee Ireine Alit"
> > $ : num 5
> > $ : chr "Marol Joseph Domingo - PS"
> > $ : chr "Kissy Andrea Arriesgado"
> > $ : chr "Pia B Baluyut, RN"
> > $ : num 6
> > $ : chr "Gladys Joy Tan"
> > $ : chr "Frances Zarzua"
> > $ : chr "Fairy Jane Nery"
> > $ : num 7
> > $ : chr "Gladys Tijam, RMT"
> > $ : chr "Sarah Jane Aramburo"
> > $ : chr "Eve Mendoza"
> > $ : num 8
> > $ : chr "Gloria Padolino"
> > $ : chr "Joyce Pearl Javier"
> > $ : chr "Ayza Padilla"
> > $ : num 9
> > $ : chr "Walfredson Calderon"
> > $ : chr "Stephanie Anne Militante"
> > $ : chr "Rennua Oquilan"
> > $ : num 10
> > $ : chr "Neil John Nery"
> > $ : chr "Maria Reyna Reyes"
> > $ : chr "Rowella Villegas"
> > $ : num 11
> > $ : chr "Katelyn Mendiola"
> > $ : chr "Maria Riza Mariano"
> > $ : chr "Marie Vallianne Carantes"
> > $ : num 12
> >
> >‐‐‐ Original Message ‐‐‐
> >
> >On Tuesday, September 14th, 2021 at 8:32 PM, Jeff Newmiller <
> jdnew...@dcn.davis.ca.us> wrote:
> >
> >> An atomic column of data by design has exactly one mode, so if any
> >> values are non-numeric then the entire column will be non-numeric.
> >> What does
> >>
> >
> >> str(VPN_Sheet1$HVA)
> >>
> >
> >> tell you? It is likely either a factor or character data.
> >>
> >
> >> On September 14, 2021 7:01:53 PM PDT, Gregg Powell via R-help
> r-help@r-project.org wrote:
> >>
> >
> >> > > Stuck on this problem - How does one remove all rows in a dataframe
> that have a numeric in the first (or any) column?
> >> >
> >
> >> > > Seems straight forward 

Re: [R] Evaluating lazily 'f<-' ?

2021-09-14 Thread Andrew Simmons
names(x) <- c("some names")

if different from

`names<-`(x, value = c("some names"))

because the second piece of code does not ever call `<-`. The first piece
of code is (approximately) equivalent to

`*tmp*` <- x
`*tmp*` <- `names<-`(`*tmp*`, value = c("some names"))
x <- `*tmp*`

Another example,

y <- `names<-`(x, value = c("some names"))

now y will be equivalent to x if we did

names(x) <- c("some names")

except that the first will not update x, it will still have its old names.

On Mon, Sep 13, 2021 at 4:33 PM Leonard Mada  wrote:

>
> On 9/13/2021 11:28 PM, Andrew Simmons wrote:
>
> In the example you gave : r(x) <- 1
> r(x) is never evaluated, the above calls `r<-`,
> in fact r does not even have to be an existing function.
>
>
> I meant:
>
> '*tmp*' <- x; # "x" is evaluated here;
>
> 'r<-' is called after this step, which makes sense in the case of
> subsetting;
>
>
> But I am wondering if changing this behaviour, when NO subsetting is
> performed, would have any impact.
>
> e.g. names(x) = c("some names");
>
> # would it have any impact to skip the evaluation of "x" and call directly:
>
> 'names<-'(x, value);
>
>
> Leonard
>
>
>
> On Mon, Sep 13, 2021, 16:18 Leonard Mada  wrote:
>
>> Hello,
>>
>>
>> I have found the evaluation: it is described in the section on
>> subsetting. The forced evaluation makes sense for subsetting.
>>
>>
>> On 9/13/2021 9:42 PM, Leonard Mada wrote:
>>
>> Hello Andrew,
>>
>>
>> I try now to understand the evaluation of the expression:
>>
>> e = expression(r(x) <- 1)
>>
>> # parameter named "value" seems to be required;
>> 'r<-' = function(x, value) {print("R");}
>> eval(e, list(x=2))
>> # [1] "R"
>>
>> # both versions work
>> 'r<-' = function(value, x) {print("R");}
>> eval(e, list(x=2))
>> # [1] "R"
>>
>>
>> ### the Expression
>> e[[1]][[1]] # "<-", not "r<-"
>> e[[1]][[2]] # "r(x)"
>>
>>
>> The evaluation of "e" somehow calls "r<-", but evaluates also the
>> argument of r(...). I am still investigating what is actually happening.
>>
>>
>> The forced evaluation is relevant for subsetting, e.g.:
>> expression(r(x)[3] <- 1)
>> expression(r(x)[3] <- 1)[[1]][[2]]
>> # r(x)[3] # the evaluation details are NOT visible in the expression per
>> se;
>> # Note: indeed, it makes sens to first evaluate r(x) and then to perform
>> the subsetting;
>>
>>
>> However, in the case of a non-subsetted expression:
>> r(x) <- 1;
>> It would make sense to evaluate lazily r(x) if no subsetting is involved
>> (more precisely "r<-"(x, value) ).
>>
>> Would this have any impact on the current code?
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>>
>> On 9/13/2021 9:15 PM, Andrew Simmons wrote:
>>
>> R's parser doesn't work the way you're expecting it to. When doing an
>> assignment like:
>>
>>
>> padding(right(df)) <- 1
>>
>>
>> it is broken into small stages. The guide "R Language Definition" claims
>> that the above would be equivalent to:
>>
>>
>> `<-`(df, `padding<-`(df, value = `right<-`(padding(df), value = 1)))
>>
>>
>> but that is not correct, and you can tell by using `substitute` as you
>> were above. There isn't a way to do what you want with the syntax you
>> provided, you'll have to do something different. You could add a `which`
>> argument to each style function, and maybe put the code for `match.arg` in
>> a separate function:
>>
>>
>> match.which <- function (which)
>> match.arg(which, c("bottom", "left", "top", "right"), several.ok = TRUE)
>>
>>
>> padding <- function (x, which)
>> {
>> which <- match.which(which)
>> # more code
>> }
>>
>>
>> border <- function (x, which)
>> {
>> which <- match.which(which)
>> # more code
>> }
>>
>>
>> some_other_style <- function (x, which)
>> {
>> which <- match.which(which)
>> # more code
>> }
>>
>>
>> I hope this helps.
>>
>> On Mon, Sep 13, 2021 a

Re: [R] How to remove all rows that have a numeric in the first (or any) column

2021-09-14 Thread Andrew Simmons
'is.numeric' is a function that returns whether its input is a numeric
vector. It looks like what you want to do is

VPN_Sheet1 <- VPN_Sheet1[!vapply(VPN_Sheet1$HVA, "is.numeric", NA), ]

instead of

VPN_Sheet1 <- VPN_Sheet1[!is.numeric(VPN_Sheet1$HVA), ]

I hope this helps, and see ?vapply if necessary.

On Tue, Sep 14, 2021 at 11:42 PM Gregg Powell via R-help <
r-help@r-project.org> wrote:

> Here is the output:
>
> > str(VPN_Sheet1$HVA)
> List of 2174
>  $ : chr "Email: f...@fff.com"
>  $ : num 1
>  $ : chr "Eloisa Libas"
>  $ : chr "Percival Esquejo"
>  $ : chr "Louchelle Singh"
>  $ : num 2
>  $ : chr "Charisse Anne Tabarno, RN"
>  $ : chr "Sol Amor Mucoy"
>  $ : chr "Josan Moira Paler"
>  $ : num 3
>  $ : chr "Anna Katrina V. Alberto"
>  $ : chr "Nenita Velarde"
>  $ : chr "Eunice Arrances"
>  $ : num 4
>  $ : chr "Catherine Henson"
>  $ : chr "Maria Carla Daya"
>  $ : chr "Renee Ireine Alit"
>  $ : num 5
>  $ : chr "Marol Joseph Domingo - PS"
>  $ : chr "Kissy Andrea Arriesgado"
>  $ : chr "Pia B Baluyut, RN"
>  $ : num 6
>  $ : chr "Gladys Joy Tan"
>  $ : chr "Frances Zarzua"
>  $ : chr "Fairy Jane Nery"
>  $ : num 7
>  $ : chr "Gladys Tijam, RMT"
>  $ : chr "Sarah Jane Aramburo"
>  $ : chr "Eve Mendoza"
>  $ : num 8
>  $ : chr "Gloria Padolino"
>  $ : chr "Joyce Pearl Javier"
>  $ : chr "Ayza Padilla"
>  $ : num 9
>  $ : chr "Walfredson Calderon"
>  $ : chr "Stephanie Anne Militante"
>  $ : chr "Rennua Oquilan"
>  $ : num 10
>  $ : chr "Neil John Nery"
>  $ : chr "Maria Reyna Reyes"
>  $ : chr "Rowella Villegas"
>  $ : num 11
>  $ : chr "Katelyn Mendiola"
>  $ : chr "Maria Riza Mariano"
>  $ : chr "Marie Vallianne Carantes"
>  $ : num 12
>
> ‐‐‐ Original Message ‐‐‐
>
> On Tuesday, September 14th, 2021 at 8:32 PM, Jeff Newmiller <
> jdnew...@dcn.davis.ca.us> wrote:
>
> > An atomic column of data by design has exactly one mode, so if any
> values are non-numeric then the entire column will be non-numeric. What does
> >
>
> > str(VPN_Sheet1$HVA)
> >
>
> > tell you? It is likely either a factor or character data.
> >
>
> > On September 14, 2021 7:01:53 PM PDT, Gregg Powell via R-help
> r-help@r-project.org wrote:
> >
>
> > > > Stuck on this problem - How does one remove all rows in a dataframe
> that have a numeric in the first (or any) column?
> > >
>
> > > > Seems straight forward - but I'm having trouble.
> > >
>
> > > I've attempted to used:
> > >
>
> > > VPN_Sheet1 <- VPN_Sheet1[!is.numeric(VPN_Sheet1$HVA),]
> > >
>
> > > and
> > >
>
> > > VPN_Sheet1 <- VPN_Sheet1[!is.integer(VPN_Sheet1$HVA),]
> > >
>
> > > Neither work - Neither throw an error.
> > >
>
> > > class(VPN_Sheet1$HVA) returns:
> > >
>
> > > [1] "list"
> > >
>
> > > So, the HVA column returns a list.
> > >
>
> > > > Data looks like the attached screen grab -
> > >
>
> > > > The ONLY rows I need to delete are the rows where there is a numeric
> in the HVA column.
> > >
>
> > > > There are some 5000+ rows in the actual data.
> > >
>
> > > > Would be grateful for a solution to this problem.
> > >
>
> > > How to get R to detect whether the value in column 1 is a number so
> the rows with the number values can be deleted?
> > >
>
> > > > Thanks in advance to any and all willing to help on this problem.
> > >
>
> > > > Gregg Powell
> > >
>
> > > > Sierra Vista, AZ
> >
>
> > --
> >
>
> > Sent from my phone. Please excuse my
> brevity.__
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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] Evaluating lazily 'f<-' ?

2021-09-13 Thread Andrew Simmons
In the example you gave : r(x) <- 1
r(x) is never evaluated, the above calls `r<-`,
in fact r does not even have to be an existing function.

On Mon, Sep 13, 2021, 16:18 Leonard Mada  wrote:

> Hello,
>
>
> I have found the evaluation: it is described in the section on subsetting.
> The forced evaluation makes sense for subsetting.
>
>
> On 9/13/2021 9:42 PM, Leonard Mada wrote:
>
> Hello Andrew,
>
>
> I try now to understand the evaluation of the expression:
>
> e = expression(r(x) <- 1)
>
> # parameter named "value" seems to be required;
> 'r<-' = function(x, value) {print("R");}
> eval(e, list(x=2))
> # [1] "R"
>
> # both versions work
> 'r<-' = function(value, x) {print("R");}
> eval(e, list(x=2))
> # [1] "R"
>
>
> ### the Expression
> e[[1]][[1]] # "<-", not "r<-"
> e[[1]][[2]] # "r(x)"
>
>
> The evaluation of "e" somehow calls "r<-", but evaluates also the argument
> of r(...). I am still investigating what is actually happening.
>
>
> The forced evaluation is relevant for subsetting, e.g.:
> expression(r(x)[3] <- 1)
> expression(r(x)[3] <- 1)[[1]][[2]]
> # r(x)[3] # the evaluation details are NOT visible in the expression per
> se;
> # Note: indeed, it makes sens to first evaluate r(x) and then to perform
> the subsetting;
>
>
> However, in the case of a non-subsetted expression:
> r(x) <- 1;
> It would make sense to evaluate lazily r(x) if no subsetting is involved
> (more precisely "r<-"(x, value) ).
>
> Would this have any impact on the current code?
>
>
> Sincerely,
>
>
> Leonard
>
>
>
> Sincerely,
>
>
> Leonard
>
>
> On 9/13/2021 9:15 PM, Andrew Simmons wrote:
>
> R's parser doesn't work the way you're expecting it to. When doing an
> assignment like:
>
>
> padding(right(df)) <- 1
>
>
> it is broken into small stages. The guide "R Language Definition" claims
> that the above would be equivalent to:
>
>
> `<-`(df, `padding<-`(df, value = `right<-`(padding(df), value = 1)))
>
>
> but that is not correct, and you can tell by using `substitute` as you
> were above. There isn't a way to do what you want with the syntax you
> provided, you'll have to do something different. You could add a `which`
> argument to each style function, and maybe put the code for `match.arg` in
> a separate function:
>
>
> match.which <- function (which)
> match.arg(which, c("bottom", "left", "top", "right"), several.ok = TRUE)
>
>
> padding <- function (x, which)
> {
> which <- match.which(which)
> # more code
> }
>
>
> border <- function (x, which)
> {
> which <- match.which(which)
> # more code
> }
>
>
> some_other_style <- function (x, which)
> {
> which <- match.which(which)
> # more code
> }
>
>
> I hope this helps.
>
> On Mon, Sep 13, 2021 at 12:17 PM Leonard Mada  wrote:
>
>> Hello Andrew,
>>
>>
>> this could work. I will think about it.
>>
>> But I was thinking more generically. Suppose we have a series of
>> functions:
>> padding(), border(), some_other_style();
>> Each of these functions has the parameter "right" (or the group of
>> parameters c("right", ...)).
>>
>>
>> Then I could design a function right(FUN) that assigns the value to this
>> parameter and evaluates the function FUN().
>>
>>
>> There are a few ways to do this:
>> 1.) Other parameters as ...
>> right(FUN, value, ...) = value; and then pass "..." to FUN.
>> right(value, FUN, ...) = value; # or is this the syntax? (TODO: explore)
>>
>> 2.) Another way:
>> right(FUN(...other parameters already specified...)) = value;
>> I wanted to explore this 2nd option: but avoid evaluating FUN, unless the
>> parameter "right" is injected into the call.
>>
>> 3.) Option 3:
>> The option you mentioned.
>>
>>
>> Independent of the method: there are still weird/unexplained behaviours
>> when I try the initial code (see the latest mail with the improved code).
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>>
>> On 9/13/2021 6:45 PM, Andrew Simmons wrote:
>>
>> I think you're trying to do something like:
>>
>> `padding<-` <- function (x, which, value)
>> {
>> which <- match.arg(which, c("bottom", "left", "top", &q

Re: [R] Evaluating lazily 'f<-' ?

2021-09-13 Thread Andrew Simmons
R's parser doesn't work the way you're expecting it to. When doing an
assignment like:


padding(right(df)) <- 1


it is broken into small stages. The guide "R Language Definition" claims
that the above would be equivalent to:


`<-`(df, `padding<-`(df, value = `right<-`(padding(df), value = 1)))


but that is not correct, and you can tell by using `substitute` as you were
above. There isn't a way to do what you want with the syntax you provided,
you'll have to do something different. You could add a `which` argument to
each style function, and maybe put the code for `match.arg` in a separate
function:


match.which <- function (which)
match.arg(which, c("bottom", "left", "top", "right"), several.ok = TRUE)


padding <- function (x, which)
{
which <- match.which(which)
# more code
}


border <- function (x, which)
{
which <- match.which(which)
# more code
}


some_other_style <- function (x, which)
{
which <- match.which(which)
# more code
}


I hope this helps.

On Mon, Sep 13, 2021 at 12:17 PM Leonard Mada  wrote:

> Hello Andrew,
>
>
> this could work. I will think about it.
>
> But I was thinking more generically. Suppose we have a series of functions:
> padding(), border(), some_other_style();
> Each of these functions has the parameter "right" (or the group of
> parameters c("right", ...)).
>
>
> Then I could design a function right(FUN) that assigns the value to this
> parameter and evaluates the function FUN().
>
>
> There are a few ways to do this:
> 1.) Other parameters as ...
> right(FUN, value, ...) = value; and then pass "..." to FUN.
> right(value, FUN, ...) = value; # or is this the syntax? (TODO: explore)
>
> 2.) Another way:
> right(FUN(...other parameters already specified...)) = value;
> I wanted to explore this 2nd option: but avoid evaluating FUN, unless the
> parameter "right" is injected into the call.
>
> 3.) Option 3:
> The option you mentioned.
>
>
> Independent of the method: there are still weird/unexplained behaviours
> when I try the initial code (see the latest mail with the improved code).
>
>
> Sincerely,
>
>
> Leonard
>
>
> On 9/13/2021 6:45 PM, Andrew Simmons wrote:
>
> I think you're trying to do something like:
>
> `padding<-` <- function (x, which, value)
> {
> which <- match.arg(which, c("bottom", "left", "top", "right"),
> several.ok = TRUE)
> # code to pad to each side here
> }
>
> Then you could use it like
>
> df <- data.frame(x=1:5, y = sample(1:5, 5))
> padding(df, "right") <- 1
>
> Does that work as expected for you?
>
> On Mon, Sep 13, 2021, 11:28 Leonard Mada via R-help 
> wrote:
>
>> I try to clarify the code:
>>
>>
>> ###
>> right = function(x, val) {print("Right");};
>> padding = function(x, right, left, top, bottom) {print("Padding");};
>> 'padding<-' = function(x, ...) {print("Padding = ");};
>> df = data.frame(x=1:5, y = sample(1:5, 5)); # anything
>>
>> ### Does NOT work as expected
>> 'right<-' = function(x, value) {
>>  print("This line should be the first printed!")
>>  print("But ERROR: x was already evaluated, which printed
>> \"Padding\"");
>>  x = substitute(x); # x was already evaluated before substitute();
>>  return("Nothing"); # do not now what the behaviour should be?
>> }
>>
>> right(padding(df)) = 1;
>>
>> ### Output:
>>
>> [1] "Padding"
>> [1] "This line should be the first printed!"
>> [1] "But ERROR: x was already evaluated, which printed \"Padding\""
>> [1] "Padding = " # How did this happen ???
>>
>>
>> ### Problems:
>>
>> 1.) substitute(x): did not capture the expression;
>> - the first parameter of 'right<-' was already evaluated, which is not
>> the case with '%f%';
>> Can I avoid evaluating this parameter?
>> How can I avoid to evaluate it and capture the expression: "right(...)"?
>>
>>
>> 2.) Unexpected
>> 'padding<-' was also called!
>> I did not know this. Is it feature or bug?
>> R 4.0.4
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>>
>> On 9/13/2021 4:45 PM, Duncan Murdoch wrote:
>> > On 13/09/2021 9:38 a.m., Leonard Mada wrote:
>> >> Hello,
>> >>
>> >>
>> >> I can include code for "padding<-"as well, but 

Re: [R] Evaluating lazily 'f<-' ?

2021-09-13 Thread Andrew Simmons
I think you're trying to do something like:

`padding<-` <- function (x, which, value)
{
which <- match.arg(which, c("bottom", "left", "top", "right"),
several.ok = TRUE)
# code to pad to each side here
}

Then you could use it like

df <- data.frame(x=1:5, y = sample(1:5, 5))
padding(df, "right") <- 1

Does that work as expected for you?

On Mon, Sep 13, 2021, 11:28 Leonard Mada via R-help 
wrote:

> I try to clarify the code:
>
>
> ###
> right = function(x, val) {print("Right");};
> padding = function(x, right, left, top, bottom) {print("Padding");};
> 'padding<-' = function(x, ...) {print("Padding = ");};
> df = data.frame(x=1:5, y = sample(1:5, 5)); # anything
>
> ### Does NOT work as expected
> 'right<-' = function(x, value) {
>  print("This line should be the first printed!")
>  print("But ERROR: x was already evaluated, which printed
> \"Padding\"");
>  x = substitute(x); # x was already evaluated before substitute();
>  return("Nothing"); # do not now what the behaviour should be?
> }
>
> right(padding(df)) = 1;
>
> ### Output:
>
> [1] "Padding"
> [1] "This line should be the first printed!"
> [1] "But ERROR: x was already evaluated, which printed \"Padding\""
> [1] "Padding = " # How did this happen ???
>
>
> ### Problems:
>
> 1.) substitute(x): did not capture the expression;
> - the first parameter of 'right<-' was already evaluated, which is not
> the case with '%f%';
> Can I avoid evaluating this parameter?
> How can I avoid to evaluate it and capture the expression: "right(...)"?
>
>
> 2.) Unexpected
> 'padding<-' was also called!
> I did not know this. Is it feature or bug?
> R 4.0.4
>
>
> Sincerely,
>
>
> Leonard
>
>
> On 9/13/2021 4:45 PM, Duncan Murdoch wrote:
> > On 13/09/2021 9:38 a.m., Leonard Mada wrote:
> >> Hello,
> >>
> >>
> >> I can include code for "padding<-"as well, but the error is before that,
> >> namely in 'right<-':
> >>
> >> right = function(x, val) {print("Right");};
> >> # more options:
> >> padding = function(x, right, left, top, bottom) {print("Padding");};
> >> 'padding<-' = function(x, ...) {print("Padding = ");};
> >> df = data.frame(x=1:5, y = sample(1:5, 5));
> >>
> >>
> >> ### Does NOT work
> >> 'right<-' = function(x, val) {
> >> print("Already evaluated and also does not use 'val'");
> >> x = substitute(x); # x was evaluated before
> >> }
> >>
> >> right(padding(df)) = 1;
> >
> > It "works" (i.e. doesn't generate an error) for me, when I correct
> > your typo:  the second argument to `right<-` should be `value`, not
> > `val`.
> >
> > I'm still not clear whether it does what you want with that fix,
> > because I don't really understand what you want.
> >
> > Duncan Murdoch
> >
> >>
> >>
> >> I want to capture the assignment event inside "right<-" and then call
> >> the function padding() properly.
> >>
> >> I haven't thought yet if I should use:
> >>
> >> padding(x, right, left, ... other parameters);
> >>
> >> or
> >>
> >> padding(x, parameter) <- value;
> >>
> >>
> >> It also depends if I can properly capture the unevaluated expression
> >> inside "right<-":
> >>
> >> 'right<-' = function(x, val) {
> >>
> >> # x is automatically evaluated when using 'f<-'!
> >>
> >> # but not when implementing as '%f%' = function(x, y);
> >>
> >> }
> >>
> >>
> >> Many thanks,
> >>
> >>
> >> Leonard
> >>
> >>
> >> On 9/13/2021 4:11 PM, Duncan Murdoch wrote:
> >>> On 12/09/2021 10:33 a.m., Leonard Mada via R-help wrote:
>  How can I avoid evaluation?
> 
>  right = function(x, val) {print("Right");};
>  padding = function(x) {print("Padding");};
>  df = data.frame(x=1:5, y = sample(1:5, 5));
> 
>  ### OK
>  '%=%' = function(x, val) {
> x = substitute(x);
>  }
>  right(padding(df)) %=% 1; # but ugly
> 
>  ### Does NOT work
>  'right<-' = function(x, val) {
> print("Already evaluated and also does not use 'val'");
> x = substitute(x); # is evaluated before
>  }
> 
>  right(padding(df)) = 1
> >>>
> >>> That doesn't make sense.  You don't have a `padding<-` function, and
> >>> yet you are trying to call right<- to assign something to padding(df).
> >>>
> >>> I'm not sure about your real intention, but assignment functions by
> >>> their nature need to evaluate the thing they are assigning to, since
> >>> they are designed to modify objects, not create new ones.
> >>>
> >>> To create a new object, just use regular assignment.
> >>>
> >>> 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

Re: [R] Calculate daily means from 5-minute interval data

2021-09-02 Thread Andrew Simmons
You could use 'split' to create a list of data frames, and then apply a
function to each to get the means and sds.


cols <- "cfs"  # add more as necessary
S <- split(discharge[cols], format(discharge$sampdate, format = "%Y-%m"))
means <- do.call("rbind", lapply(S, colMeans, na.rm = TRUE))
sds   <- do.call("rbind", lapply(S, function(xx) sapply(xx, sd, na.rm =
TRUE)))

On Thu, Sep 2, 2021 at 3:01 PM Rich Shepard 
wrote:

> On Thu, 2 Sep 2021, Rich Shepard wrote:
>
> > If I correctly understand the output of as.POSIXlt each date and time
> > element is separate, so input such as 2016-03-03 12:00 would now be 2016
> 03
> > 03 12 00 (I've not read how the elements are separated). (The TZ is not
> > important because all data are either PST or PDT.)
>
> Using this script:
> discharge <- read.csv('../data/water/discharge.dat', header = TRUE, sep =
> ',', stringsAsFactors = FALSE)
> discharge$sampdate <- as.POSIXlt(discharge$sampdate, tz = "",
>   format = '%Y-%m-%d %H:%M',
>   optional = 'logical')
> discharge$cfs <- as.numeric(discharge$cfs, length = 6)
>
> I get this result:
> > head(discharge)
>   sampdatecfs
> 1 2016-03-03 12:00:00 149000
> 2 2016-03-03 12:10:00 15
> 3 2016-03-03 12:20:00 151000
> 4 2016-03-03 12:30:00 156000
> 5 2016-03-03 12:40:00 154000
> 6 2016-03-03 12:50:00 15
>
> I'm completely open to suggestions on using this output to calculate
> monthly
> means and sds.
>
> If dplyr:summarize() will do so please show me how to modify this command:
> disc_monthly <- ( discharge
>  %>% group_by(sampdate)
>  %>% summarize(exp_value = mean(cfs, na.rm = TRUE))
> because it produces daily means, not monthly means.
>
> TIA,
>
> Rich
>
> __
> 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] How to globally convert NaN to NA in dataframe?

2021-09-02 Thread Andrew Simmons
It seems like you might've missed one more thing, you need the brackets
next to 'x' to get it to work.


x[] <- lapply(x, function(xx) {
xx[is.nan(xx)] <- NA_real_
xx
})

is different from

x <- lapply(x, function(xx) {
xx[is.nan(xx)] <- NA_real_
xx
})

Also, if all of your data is numeric, it might be better to convert to a
matrix before doing your calculations. For example:

x <- as.matrix(x)
x[is.nan(x)] <- NA_real_

I'd also suggest this same solution for the other question you posted,

x[x == 0] <- NA

On Thu, Sep 2, 2021 at 10:01 AM Luigi Marongiu 
wrote:

> Sorry,
> still I don't get it:
> ```
> > dim(df)
> [1] 302 626
> > # clean
> > df <- lapply(x, function(xx) {
> +   xx[is.nan(xx)] <- NA
> +   xx
> + })
> > dim(df)
> NULL
> ```
>
> On Thu, Sep 2, 2021 at 3:47 PM Andrew Simmons  wrote:
> >
> > You removed the second line 'xx' from the function, put it back and it
> should work
> >
> > On Thu, Sep 2, 2021, 09:45 Luigi Marongiu 
> wrote:
> >>
> >> `data[sapply(data, is.nan)] <- NA` is a nice compact command, but I
> >> still get NaN when using the summary function, for instance one of the
> >> columns give:
> >> ```
> >> Min.   : NA
> >> 1st Qu.: NA
> >> Median : NA
> >> Mean   :NaN
> >> 3rd Qu.: NA
> >> Max.   : NA
> >> NA's   :110
> >> ```
> >> I tried to implement the second solution but:
> >> ```
> >> df <- lapply(x, function(xx) {
> >>   xx[is.nan(xx)] <- NA
> >> })
> >> > str(df)
> >> List of 1
> >>  $ sd_ef_rash_loc___palm: logi NA
> >> ```
> >> What am I getting wrong?
> >> Thanks
> >>
> >> On Thu, Sep 2, 2021 at 3:30 PM Andrew Simmons 
> wrote:
> >> >
> >> > Hello,
> >> >
> >> >
> >> > I would use something like:
> >> >
> >> >
> >> > x <- c(1:5, NaN) |> sample(100, replace = TRUE) |> matrix(10, 10) |>
> as.data.frame()
> >> > x[] <- lapply(x, function(xx) {
> >> > xx[is.nan(xx)] <- NA_real_
> >> > xx
> >> > })
> >> >
> >> >
> >> > This prevents attributes from being changed in 'x', but accomplishes
> the same thing as you have above, I hope this helps!
> >> >
> >> > On Thu, Sep 2, 2021 at 9:19 AM Luigi Marongiu <
> marongiu.lu...@gmail.com> wrote:
> >> >>
> >> >> Hello,
> >> >> I have some NaN values in some elements of a dataframe that I would
> >> >> like to convert to NA.
> >> >> The command `df1$col[is.nan(df1$col)]<-NA` allows to work
> column-wise.
> >> >> Is there an alternative for the global modification at once of all
> >> >> instances?
> >> >> I have seen from
> >> >>
> https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097
> >> >> that once could use:
> >> >> ```
> >> >>
> >> >> is.nan.data.frame <- function(x)
> >> >> do.call(cbind, lapply(x, is.nan))
> >> >>
> >> >> data123[is.nan(data123)] <- 0
> >> >> ```
> >> >> replacing o with NA, but I got
> >> >> ```
> >> >> str(df)
> >> >> > logi NA
> >> >> ```
> >> >> when modifying my dataframe df.
> >> >> What would be the correct syntax?
> >> >> Thank you
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> Best regards,
> >> >> Luigi
> >> >>
> >> >> __
> >> >> 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.
> >>
> >>
> >>
> >> --
> >> Best regards,
> >> Luigi
>
>
>
> --
> Best regards,
> Luigi
>

[[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] How to globally convert NaN to NA in dataframe?

2021-09-02 Thread Andrew Simmons
You removed the second line 'xx' from the function, put it back and it
should work

On Thu, Sep 2, 2021, 09:45 Luigi Marongiu  wrote:

> `data[sapply(data, is.nan)] <- NA` is a nice compact command, but I
> still get NaN when using the summary function, for instance one of the
> columns give:
> ```
> Min.   : NA
> 1st Qu.: NA
> Median : NA
> Mean   :NaN
> 3rd Qu.: NA
> Max.   : NA
> NA's   :110
> ```
> I tried to implement the second solution but:
> ```
> df <- lapply(x, function(xx) {
>   xx[is.nan(xx)] <- NA
> })
> > str(df)
> List of 1
>  $ sd_ef_rash_loc___palm: logi NA
> ```
> What am I getting wrong?
> Thanks
>
> On Thu, Sep 2, 2021 at 3:30 PM Andrew Simmons  wrote:
> >
> > Hello,
> >
> >
> > I would use something like:
> >
> >
> > x <- c(1:5, NaN) |> sample(100, replace = TRUE) |> matrix(10, 10) |>
> as.data.frame()
> > x[] <- lapply(x, function(xx) {
> > xx[is.nan(xx)] <- NA_real_
> > xx
> > })
> >
> >
> > This prevents attributes from being changed in 'x', but accomplishes the
> same thing as you have above, I hope this helps!
> >
> > On Thu, Sep 2, 2021 at 9:19 AM Luigi Marongiu 
> wrote:
> >>
> >> Hello,
> >> I have some NaN values in some elements of a dataframe that I would
> >> like to convert to NA.
> >> The command `df1$col[is.nan(df1$col)]<-NA` allows to work column-wise.
> >> Is there an alternative for the global modification at once of all
> >> instances?
> >> I have seen from
> >>
> https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097
> >> that once could use:
> >> ```
> >>
> >> is.nan.data.frame <- function(x)
> >> do.call(cbind, lapply(x, is.nan))
> >>
> >> data123[is.nan(data123)] <- 0
> >> ```
> >> replacing o with NA, but I got
> >> ```
> >> str(df)
> >> > logi NA
> >> ```
> >> when modifying my dataframe df.
> >> What would be the correct syntax?
> >> Thank you
> >>
> >>
> >>
> >> --
> >> Best regards,
> >> Luigi
> >>
> >> __
> >> 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.
>
>
>
> --
> Best regards,
> Luigi
>

[[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] How to globally convert NaN to NA in dataframe?

2021-09-02 Thread Andrew Simmons
Hello,


I would use something like:


x <- c(1:5, NaN) |> sample(100, replace = TRUE) |> matrix(10, 10) |>
as.data.frame()
x[] <- lapply(x, function(xx) {
xx[is.nan(xx)] <- NA_real_
xx
})


This prevents attributes from being changed in 'x', but accomplishes the
same thing as you have above, I hope this helps!

On Thu, Sep 2, 2021 at 9:19 AM Luigi Marongiu 
wrote:

> Hello,
> I have some NaN values in some elements of a dataframe that I would
> like to convert to NA.
> The command `df1$col[is.nan(df1$col)]<-NA` allows to work column-wise.
> Is there an alternative for the global modification at once of all
> instances?
> I have seen from
>
> https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097
> that once could use:
> ```
>
> is.nan.data.frame <- function(x)
> do.call(cbind, lapply(x, is.nan))
>
> data123[is.nan(data123)] <- 0
> ```
> replacing o with NA, but I got
> ```
> str(df)
> > logi NA
> ```
> when modifying my dataframe df.
> What would be the correct syntax?
> Thank you
>
>
>
> --
> Best regards,
> Luigi
>
> __
> 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] Converting characters back to Date and Time

2021-08-31 Thread Andrew Simmons
Hello,


I'm assuming you're reading from an "*.xlsx" file. I'm not sure which
package you're using for this scenario, but my preference is 'openxlsx'. If
this is the package you're using, you could set argument 'detectDates' to
'TRUE', and then it should read them correctly.


FILE <- tempfile(fileext = ".xlsx")


openxlsx::write.xlsx(
data.frame(V1 = as.Date("2000-01-01")),
FILE
)


openxlsx::read.xlsx(FILE)
openxlsx::read.xlsx(FILE, detectDates = TRUE)


unlink(FILE)


The first one should read the dates as numbers (days since 1900-01-01 or
1904-01-01, depending upon setup), while the second should parse them to
class "Date". I hope this helps!

On Tue, Aug 31, 2021 at 4:26 PM Eliza Botto  wrote:

> DeaR useR,
>
> I read an excel column in R having Date and time (written in the same
> cell) as follow,
>
> 06/18/18 10:00
>
> 06/18/18 11:00
>
> 06/18/18 12:00
>
> In R environment, they are read as
>
> 43269.42
>
> 43269.46
>
> 43269.50
>
> Is there a way to covert these characters back to the original format?
>
> Thank-you very much in advance.
>
>
> Eliza Botto
>
> [[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] Calculate daily means from 5-minute interval data

2021-08-29 Thread Andrew Simmons
Hello,


I would suggest something like:


date <- seq(as.Date("2020-01-01"), as.Date("2020-12-31"), 1)
time <- sprintf("%02d:%02d", rep(0:23, each = 12), seq.int(0, 55, 5))
x <- data.frame(
date = rep(date, each = length(time)),
time = time
)
x$cfs <- stats::rnorm(nrow(x))


cols2aggregate <- "cfs"  # add more as necessary


S <- split(x[cols2aggregate], x$date)


means <- do.call("rbind", lapply(S, colMeans, na.rm = TRUE))
sds   <- do.call("rbind", lapply(S, function(xx) sapply(xx, sd, na.rm =
TRUE)))

On Sun, Aug 29, 2021 at 11:09 AM Rich Shepard 
wrote:

> I have a year's hydraulic data (discharge, stage height, velocity, etc.)
> from a USGS monitoring gauge recording values every 5 minutes. The data
> files contain 90K-93K lines and plotting all these data would produce a
> solid block of color.
>
> What I want are the daily means and standard deviation from these data.
>
> As an occasional R user (depending on project needs) I've no idea what
> packages could be applied to these data frames. There likely are multiple
> paths to extracting these daily values so summary statistics can be
> calculated and plotted. I'd appreciate suggestions on where to start to
> learn how I can do this.
>
> TIA,
>
> Rich
>
> __
> 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] Finding if numbers fall within a range

2021-08-28 Thread Andrew Simmons
Hello,


I think I've found a solution, but it's not producing the same answer as
what you're expecting. I think you might've mixed up row and column a few
times, but you should be able to alter the following to your needs. Also,
see ?.bincode:


EB <- matrix(data = c(
  1, 271, 179, 359, 178,
 57, 279, 279,  52, 239,
 59,  59, 278, 118, 334
), nrow = 3, ncol = 5, byrow = TRUE)


# ranges <- list(
# c(  0,  60),
# c( 61, 120),
# c(121, 180),
# c(181, 240),
# c(241, 300),
# c(301, 360)
# )
breaks <- seq.int(0, 360, 60)


codes <- .bincode(EB, breaks, include.lowest = TRUE)
dim(codes) <- dim(EB)


num.ranges <- apply(codes, 1, function(xx) length(unique(xx)))


I hope this helps!

On Sat, Aug 28, 2021 at 11:57 AM Eliza Botto 
wrote:

> Dear useRs,
>
> Is there a way in R to see if the numbers in a matrix-row fall within the
> given range or not? For example, I have the following data;
>
> > dput(EB)
>
> structure(c(1, 57, 59, 271, 279, 59, 179, 279, 278, 359, 52,
> 118, 178, 239, 334), .Dim = c(3L, 5L))
>
> The ranges for which these numbers are to be reserved are;
>
> 0-60, 61-120, 121-180, 181-240, 241-300, 301-360
>
> In row 1, the number "1", "57", and "59" fall 2ith the range 0-60. Whereas
> "271" and "279" falls within the range 241-300. So in total 2 ranges are
> covered and coding should execute 2 for row one.
>
> In row 2, the number "59" falls in the range 0-60, "179" falls in the
> range 121-180, "279" and "278" fall within the range 241-300, and 359 falls
> in the range 301-360. In total 4 ranges are covered for row 2.
>
> In the like-wise pattern, 5 ranges are covered in row 3.
>
> So, what I want is a matrix that looks in the following
>
> > dput(EBI)
>
> structure(c(2, 4, 5), .Dim = c(3L, 1L))
>
> Is there an easy way of doing it?
>
> I thank you all very much in advance
>
> EB
>
> [[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] Potential bug/unexpected behaviour in model matrix

2021-08-26 Thread Andrew Simmons
Hello,


I'm not so sure this is a bug, it appears to be behaving as intended from
the documentation. I would suggest using argument 'physical' from 'setkey'
to avoid reordering the rows. Something like:


x <- data.table::data.table(V1 = 9:0)
y <- data.table::copy(x)


data.table::setkey(x, V1, physical = TRUE)
data.table::setkey(y, V1, physical = FALSE)


print(x)
print(y)


attr(x, "index")
attr(y, "index")


'x' does not have an attribute index because the rows were reordered. 'y'
does have an index because its rows weren't reordered. I hope this helps!



On Thu, Aug 26, 2021 at 1:02 PM Leonidas Lundell 
wrote:

> Dear R-project,
>
> Apologies if I am sending this to the wrong list, and thank you for your
> enormous contribution.
>
> I discovered a subtle interaction between the data.table package and
> model.matrix function that influences the output to the point that you will
> get completely erroneous results:
>
> df  <- data.frame(basespaceID = 8:1, group = paste0(rep(c("a", "b"), 4),
> "_", sort(rep(c("1", "2"), 4
> designDF <- model.matrix(~0 + group, data = df)
>
> dt <- data.table::as.data.table(df)
> designDT <- model.matrix(~0 + group, data = dt)
>
> all(designDF == designDT)
> #TRUE
>
> data.table::setkey(dt, "basespaceID")
> designDTkeyed <- model.matrix(~0 + group, data = dt)
>
> all(designDF == designDTkeyed)
> #FALSE
>
> # It seems that a keyed data.table reorders the rows of the design matrix
> by alphabetical order:
>
>  designDFreordered <- model.matrix(~0 + group, data = df[8:1,])
> all(designDFreordered == designDTkeyed)
> #TRUE
>
> And my sessionInfo if that’s of any help:
>
> sessionInfo()
>
> R version 4.1.0 (2021-05-18)
> Platform: x86_64-apple-darwin17.0 (64-bit)
> Running under: macOS Big Sur 11.5.2
>
> Matrix products: default
> LAPACK:
> /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> other attached packages:
> [1] data.table_1.14.0
>
> loaded via a namespace (and not attached):
> [1] umap_0.2.7.0  Rcpp_1.0.7knitr_1.33magrittr_2.0.1
>  [5] maps_3.3.0lattice_0.20-44   rlang_0.4.11
> stringr_1.4.0
>  [9] tools_4.1.0   grid_4.1.0xfun_0.25
> png_0.1-7
> [13] audio_0.1-7   RSpectra_0.16-0   htmltools_0.5.1.1
> shapefiles_0.7
> [17] askpass_1.1   openssl_1.4.4 yaml_2.2.1
> digest_0.6.27
> [21] zip_2.2.0 Matrix_1.3-4  beepr_1.3
> evaluate_0.14
> [25] rmarkdown_2.10openxlsx_4.2.4sp_1.4-5
> stringi_1.7.3
> [29] compiler_4.1.0fossil_0.4.0  jsonlite_1.7.2
> reticulate_1.20
> [33] foreign_0.8-81
>
> Best regards
>
> Leonidas Lundell
> Postdoc
> Barres & Zierath group
>
> University of Copenhagen
> Novo Nordisk Foundation
> Center for Basic Metabolic Research
>
> mailto:leo.lund...@sund.ku.dk
>
>
>
>
>
> __
> 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] ggplot error of "`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class rxlsx"

2021-08-26 Thread Andrew Simmons
Hello,


Package 'officer' has a function 'read_xlsx', so when you attach those
packages in that order, it returns 'read_xlsx' from package 'officer'
instead of 'readxl'. To avoid the confusion, instead of

eth <- read_xlsx("c:/temp/eth.xlsx")

try

eth <- readxl::read_xlsx("c:/temp/eth.xlsx")

which will always refer to the correct function. I can't seem to reproduce
the original error you have, I tried something like:

openxlsx::write.xlsx(data.frame(x = 1:26, y = stats::rnorm(26)), FILE <-
tempfile())
eth <- readxl::read_xlsx(FILE)
eth2 <- as.data.frame(eth)
ggplot2::ggplot(eth2, ggplot2::aes(x = x, y = y))


unlink(FILE)

which just worked as expected.

On Thu, Aug 26, 2021 at 3:03 PM Kai Yang via R-help 
wrote:

>  Hi all,
> I found something, but I don't know why it happen.
> when I submitted the following code, the Eth is data frame. I can see 14
> obs. of 2 variables
> library(readxl)
> library(ggplot2)
> eth <- read_xlsx("c:/temp/eth.xlsx")
>
>
> but when I add more package (see below,) the Eth is "List of 1"
> library(readxl)
> library(ggplot2)
> library(dplyr)
> library(magrittr)
> library(knitr)
> library(xtable)
> library(flextable)
> library(officer)
> eth <- read_xlsx("c:/temp/eth.xlsx")
>
> But I need those package in future. Is there a way to fix the problem?
> Thanks,
> KaiOn Thursday, August 26, 2021, 11:37:53 AM PDT, Kai Yang via R-help <
> r-help@r-project.org> wrote:
>
>   Hi All,
> 1. the eth is a data frame (not sure that based on error message?) that I
> load it from excel file. Here is the code: eth <-
> read_xlsx("c:/temp/eth.xlsx")
> 2. I try to use the code to convert eth into eth2, but I got error message:
> > eth2 <- data.frame(eth)
> Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors =
> stringsAsFactors) :
>   cannot coerce class ‘"rxlsx"’ to a data.frame
>
> So, it seems the data.frame can not do this data convert? Do you know
> which statement/function can do this?
>
>
> thank you for your help.
>
> On Thursday, August 26, 2021, 09:33:51 AM PDT, Avi Gross via R-help <
> r-help@r-project.org> wrote:
>
>  Kai,
>
> The answer is fairly probable to find  if you examine your variable "eth"
> as that is the only time you are being asked to provide the argument as in
> "ggplot(data=eth, ..) ...)
>
> As the message states, it expects that argument to be a data frame or
> something it can change into a data.frame. What you gave it probably is an
> object meant to represent an EXCEL file or something. You may need to
> extract a data.frame (or tibble or ...) from it before passing that to
> ggplot.
>
> Avi
>
> -Original Message-
> From: R-help  On Behalf Of Kai Yang via
> R-help
> Sent: Thursday, August 26, 2021 11:53 AM
> To: R-help Mailing List 
> Subject: [R] ggplot error of "`data` must be a data frame, or other object
> coercible by `fortify()`, not an S3 object with class rxlsx"
>
> Hello List,
> I got an error message when I submit the code below ggplot(eth,
> aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=ethnicity)) +  geom_rect()
> +  coord_polar(theta="y")  +  xlim(c(2, 4)  )
>
> Error: `data` must be a data frame, or other object coercible by
> `fortify()`, not an S3 object with class rxlsx
>
>
> I checked the syntax. But I can  not find any error on my code. Can you
> help me to find where is the problem?
>
> Thanks
>
> [[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.
>
> [[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.
>

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

Re: [R] ggplot error of "`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class rxlsx"

2021-08-26 Thread Andrew Simmons
The class of 'eth' must be incorrect. You could try 'as.data.frame' or
possibly 'as.list' to convert 'eth' to an acceptable form.

On Thu, Aug 26, 2021, 11:53 Kai Yang via R-help 
wrote:

> Hello List,
> I got an error message when I submit the code below
> ggplot(eth, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=ethnicity)) +
> geom_rect() +  coord_polar(theta="y")  +  xlim(c(2, 4)   )
>
> Error: `data` must be a data frame, or other object coercible by
> `fortify()`, not an S3 object with class rxlsx
>
>
> I checked the syntax. But I can  not find any error on my code. Can you
> help me to find where is the problem?
>
> Thanks
>
> [[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] Need help to unzip files in Windows

2021-08-23 Thread Andrew Simmons
Hello,


I see what you're saying that the .tar archive contains many more
compressed files, but that's not necessarily a problem. R can read directly
from a compressed file without having to decompress it beforehand. I
modified my code to look a little more like yours:


# need to do 'path.expand' or 'untar' will fail
# this is where we put the downloaded files
exdir <- path.expand("~/GSE162562_RAW")
dir.create(exdir, showWarnings = FALSE)


URL <- "
https://ftp.ncbi.nlm.nih.gov/geo/series/GSE162nnn/GSE162562/suppl/GSE162562_RAW.tar
"
FILE <- file.path(tempdir(), basename(URL))


utils::download.file(URL, FILE, mode = "wb")
utils::untar(FILE, exdir = exdir)
unlink(FILE, recursive = TRUE, force = TRUE)


# 'files' is the full path to the downloaded files
# attribute 'names' is the basename with '.txt.gz' removed from the end
files <- list.files(exdir, full.names = TRUE)
names(files) <- sub("\\.txt\\.gz$", "", basename(files))


# R can open compressed files without decompressing beforehand
print(utils::read.table(files[[1]], sep = "\t"))
print(utils::read.delim(files[[2]], header = FALSE))


Does this work better than before for you?

On Mon, Aug 23, 2021 at 8:16 PM Anas Jamshed 
wrote:

> sir after that I want to run:
> #get the list of sample names
> GSMnames <- t(list.files("~/Desktop/GSE162562_RAW", full.names = F))
>
> #remove .txt from file/sample names
> GSMnames <- gsub(pattern = ".txt", replacement = "", GSMnames)
>
> #make a vector of the list of files to aggregate
> files <- list.files("~/Desktop/GSE162562_RAW", full.names = TRUE)
>
>
> but it is not running as after running utils::untar(FILE, exdir =
> dirname(FILE)) it creates another 108 archieves
>
> On Tue, Aug 24, 2021 at 2:03 AM Andrew Simmons  wrote:
>
>> Hello,
>>
>>
>> I tried downloading that file using 'utils::download.file' (which
>> worked), but then continued to complain about "damaged archive" when trying
>> to use 'utils::untar'. However, it seemed to work when I downloaded the
>> archive manually. Finally, the solution I found is that you have to specify
>> the mode in which you're downloading the file. Something like:
>>
>>
>> URL <- "
>> https://ftp.ncbi.nlm.nih.gov/geo/series/GSE162nnn/GSE162562/suppl/GSE162562_RAW.tar
>> "
>> FILE <- file.path(tempdir(), basename(URL))
>>
>>
>> utils::download.file(URL, FILE, mode = "wb")
>> utils::untar(FILE, exdir = dirname(FILE))
>>
>>
>> worked perfectly for me. It seems to also work still on Ubuntu, but you
>> can let us know if you find it doesn't. I hope this helps!
>>
>>
>>
>> On Mon, Aug 23, 2021 at 3:20 PM Anas Jamshed 
>> wrote:
>>
>>> I am trying this URL: "
>>> https://ftp.ncbi.nlm.nih.gov/geo/series/GSE162nnn/GSE162562/suppl/GSE162562_RAW.tar
>>> "
>>>
>>> but it is not giving me any file
>>>
>>> On Mon, Aug 23, 2021 at 11:42 PM Andrew Simmons 
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>>
>>>> I don't think you need to use a system command directly, I think
>>>> 'utils::untar' is all you need. I tried the same thing myself, something
>>>> like:
>>>>
>>>>
>>>> URL <- "https://exiftool.org/Image-ExifTool-12.30.tar.gz;
>>>> FILE <- file.path(tempdir(), basename(URL))
>>>>
>>>>
>>>> utils::download.file(URL, FILE)
>>>> utils::untar(FILE, exdir = dirname(FILE))
>>>>
>>>>
>>>> and it makes a folder "Image-ExifTool-12.30". It seems to work
>>>> perfectly fine in Windows 10 x64 build 19042. Can you send the specific
>>>> file (or provide a URL to the specific file) that isn't working for you?
>>>>
>>>> On Mon, Aug 23, 2021 at 12:53 PM Anas Jamshed <
>>>> anasjamshed1...@gmail.com> wrote:
>>>>
>>>>> I have the file GSE162562_RAW. First I untar them
>>>>> by untar("GSE162562_RAW.tar")
>>>>> then I am running like:
>>>>>  system("gunzip ~/Desktop/GSE162562_RAW/*.gz")
>>>>>
>>>>>
>>>>> This is running fine in Linux but not in windows. What changes I
>>>>> should make to run this command in windows as well
>>>>>
>>>>> [[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] Need help to unzip files in Windows

2021-08-23 Thread Andrew Simmons
Hello,


I tried downloading that file using 'utils::download.file' (which worked),
but then continued to complain about "damaged archive" when trying to use
'utils::untar'. However, it seemed to work when I downloaded the archive
manually. Finally, the solution I found is that you have to specify the
mode in which you're downloading the file. Something like:


URL <- "
https://ftp.ncbi.nlm.nih.gov/geo/series/GSE162nnn/GSE162562/suppl/GSE162562_RAW.tar
"
FILE <- file.path(tempdir(), basename(URL))


utils::download.file(URL, FILE, mode = "wb")
utils::untar(FILE, exdir = dirname(FILE))


worked perfectly for me. It seems to also work still on Ubuntu, but you can
let us know if you find it doesn't. I hope this helps!



On Mon, Aug 23, 2021 at 3:20 PM Anas Jamshed 
wrote:

> I am trying this URL: "
> https://ftp.ncbi.nlm.nih.gov/geo/series/GSE162nnn/GSE162562/suppl/GSE162562_RAW.tar
> "
>
> but it is not giving me any file
>
> On Mon, Aug 23, 2021 at 11:42 PM Andrew Simmons 
> wrote:
>
>> Hello,
>>
>>
>> I don't think you need to use a system command directly, I think
>> 'utils::untar' is all you need. I tried the same thing myself, something
>> like:
>>
>>
>> URL <- "https://exiftool.org/Image-ExifTool-12.30.tar.gz;
>> FILE <- file.path(tempdir(), basename(URL))
>>
>>
>> utils::download.file(URL, FILE)
>> utils::untar(FILE, exdir = dirname(FILE))
>>
>>
>> and it makes a folder "Image-ExifTool-12.30". It seems to work perfectly
>> fine in Windows 10 x64 build 19042. Can you send the specific file (or
>> provide a URL to the specific file) that isn't working for you?
>>
>> On Mon, Aug 23, 2021 at 12:53 PM Anas Jamshed 
>> wrote:
>>
>>> I have the file GSE162562_RAW. First I untar them
>>> by untar("GSE162562_RAW.tar")
>>> then I am running like:
>>>  system("gunzip ~/Desktop/GSE162562_RAW/*.gz")
>>>
>>>
>>> This is running fine in Linux but not in windows. What changes I
>>> should make to run this command in windows as well
>>>
>>> [[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] Need help to unzip files in Windows

2021-08-23 Thread Andrew Simmons
Hello,


I don't think you need to use a system command directly, I think
'utils::untar' is all you need. I tried the same thing myself, something
like:


URL <- "https://exiftool.org/Image-ExifTool-12.30.tar.gz;
FILE <- file.path(tempdir(), basename(URL))


utils::download.file(URL, FILE)
utils::untar(FILE, exdir = dirname(FILE))


and it makes a folder "Image-ExifTool-12.30". It seems to work perfectly
fine in Windows 10 x64 build 19042. Can you send the specific file (or
provide a URL to the specific file) that isn't working for you?

On Mon, Aug 23, 2021 at 12:53 PM Anas Jamshed 
wrote:

> I have the file GSE162562_RAW. First I untar them
> by untar("GSE162562_RAW.tar")
> then I am running like:
>  system("gunzip ~/Desktop/GSE162562_RAW/*.gz")
>
>
> This is running fine in Linux but not in windows. What changes I
> should make to run this command in windows as well
>
> [[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.


[R] Returning .Call / / .External results invisibly

2021-08-15 Thread Andrew Simmons
Hello,


I have a C function in which I want to return a result visibly or invisibly
(depends on the arguments provided). My current implementation was to
return a list like 'withVisible' does, where element "value" is the value
the function returns, and element "visible" is TRUE or FALSE depending on
whether the result is returned visibly or not. Something like:

{
value <- .External(C_fun, ...)
if (value$visible)
value$value
else invisible(value$value)
}

Is there a way to do this in C instead? Thank you!

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Andrew Simmons
Hello,


There are two convenient ways to access a column in a data.frame using `$`
and `[[`. Using `df` from your first email, we would do something like

df <- data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red",
"empty"))
df$VAL
df[["VAL"]]

The two convenient ways to update / / replace a column with something new
are also very similar, something like

df$VAL <- ...
df[["VAL"]] <- ...

As for the regex part, I would suggest using `sub` instead of `gsub` since
you're looking to remove only the first instance of "value is". Also, I
would recommend using "^" to mark the beginning of your string, something
like

df$VAL <- sub("^Value is ", "", df$VAL, ignore.case = TRUE)

I might be misunderstanding, but it sounds like you also want to remove all
leading whitespace. If so, you could do something like

df$VAL <- sub("^[[:blank:]]*Value is ", "", df$VAL, ignore.case = TRUE)

where "*" signifies that there will be zero or more blank characters at the
beginning of the string. You can try `?regex` to read more about this.

I hope this helps!

On Mon, Aug 9, 2021 at 6:50 AM Luigi Marongiu 
wrote:

> Sorry, silly question, gsub works already with regex. But still, if I
> add `[[:blank:]]` still I don't get rid of all instances. And I am
> keeping obtaining extra columns
> ```
> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE)
> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE);df
>   VAR   VAL value is blue Value is red empty
> 1   1 value is blue bb b
> 2   2  Value is redrd   rdrd
> 3   3 empty  mpty mpty  mpty
> ```
>
> On Mon, Aug 9, 2021 at 12:40 PM Luigi Marongiu 
> wrote:
> >
> > Thank you, that is much appreciated. But on the real data, the
> > substitution works only on few instances. Is there a way to introduce
> > regex into this?
> > Cheers
> > Luigi
> >
> > On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
> > >
> > > Hi Luigi,
> > > Ah, now I see:
> > >
> > >  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> > > df
> > >  VAR   VAL
> > > 1   1  blue
> > > 2   2   red
> > > 3   3 empty
> > >
> > > Jim
> > >
> > > On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu <
> marongiu.lu...@gmail.com> wrote:
> > > >
> > > > Hello,
> > > > I have a dataframe where I would like to change the string of certain
> > > > rows, essentially I am looking to remove some useless text from the
> > > > variables.
> > > > I tried with:
> > > > ```
> > > > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is
> red", "empty"))
> > > > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE,
> perl = FALSE)
> > > > > df
> > > >   VAR   VAL value is blue Value is red empty
> > > > 1   1 value is blue  blue blue  blue
> > > > 2   2  Value is red   red  red   red
> > > > 3   3 empty emptyempty empty
> > > > ```
> > > > which is of course wrong because I was expecting
> > > > ```
> > > >   VAR   VAL
> > > > 1   1 blue
> > > > 2   2 red
> > > > 3   3empty
> > > > ```
> > > > What is the correct syntax in these cases?
> > > > 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.
> >
> >
> >
> > --
> > Best regards,
> > Luigi
>
>
>
> --
> Best regards,
> Luigi
>
> __
> 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] Puzzled over "partial"

2021-07-26 Thread Andrew Simmons
Hello,


First, your statement can be re-written as

sort(x,partial=p)[p]

since n - (n - p) is p. Second, you need to look at

?sort

And look at section "Arguments" subsection "partial", that should have the
details you're looking for. From what I understand, it guarantees that the
indices of "partial" will be sorted correctly, but all others won't, so
this saves time for things like "median". I hope this helps!


On Mon, Jul 26, 2021, 07:54 Nick Wray  wrote:

> Hello   I am puzzled about the use or status of "partial" in R.  years ago
> I found a little piece of code which gives the pth largest number in a
> vector:
> x<-c(5,4,7,2,6,9)
> n <- length(x)
> p<-4
> sort(x,partial=n-(n-p))[n-(n-p)]
> This works fine, although I have tried playing around with the code and
> don't understand what "partial" is doing here.
> However, wanted to work out what was going on, so I looked for "partial in
> r" on t'internet and got this site:Partial apply a function, filling in
> some arguments. — partial • purrr (tidyverse.org)
> <
> https://purrr.tidyverse.org/reference/partial.html#:~:text=Source%3A%20R%2Fpartial.R%20Partial%20function%20application%20allows%20you%20to,that%20an%20argument%20can%20only%20be%20partialised%20once
> .>
> Examples:
> # Partial is designed to replace the use of anonymous functions for #
> filling in function arguments. Instead of: compact1 <- function(x) discard
> (x, is.null) # we can
> write: compact2 <- partial(discard, .p = is.null) # partial() works fine
> with functions that do non-standard # evaluation my_long_variable <- 1:10
> plot2 <- partial(plot, my_long_variable) plot2()
> when i tried to run the examples on this site I got error messages - R
> (studio) did not recognise the "partial" function here.  The site did not
> say that I needed a particular package to run the "partial" function.
> Are there essentially two different things in R both described as "partial"
> but which are actually different entities?
> Thanks for any elucidation
> Nick Wray
>
> [[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.


[R] Testing for R CMD INSTALL

2021-07-24 Thread Andrew Simmons
Hello,


I was wondering if anyone has a way to test if a package is currently being
installed. My solution was to check if environment variable "R_INSTALL_PKG"
was unset, something like:

"R CMD INSTALL-ing" <- function ()
!is.na(Sys.getenv("R_INSTALL_PKG", NA))

Unfortunately, I couldn't find what I was looking for with ?"environment
variables". So if anyone has any better methods, I'd be happy to hear them,
thank you!

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with Converting Excel Times to R

2021-07-21 Thread Andrew Simmons
Hello,


>From playing around with your numbers, it seems like you are using Excel
1904 Date System, which isn't a problem, it just means that your numbers
are days from 1904-01-01 instead of 1900-01-01.
The following is my solution:

times <- c(42935.5625,42935.569444)
as.POSIXlt((


# offset the days by the origin for Excel
times + unclass(as.Date("1904-01-01"))


# multiply by 86400 to convert number of days to number of seconds
) * 86400,


# .Date(0) is the origin day for R
origin = .Date(0), tz = "GMT")

though a better way to do it would be to save the datetimes in Excel as
text, and then use `as.POSIXct(..., format = ...)` on the text field once
read into R. Something like:


times <- c("7/20/2021 13:30", "7/20/2021 13:40")
as.POSIXlt(times, format = "%m/%d/%Y %H:%M", tz = "GMT")


On Wed, Jul 21, 2021 at 6:49 PM Shawn Way  wrote:

> I've usually had good luck with this, but something is not working well.
> I have two datetimes in excel
>
> 7/20/21 13:30
> 7/20/21 13:40
>
> And when I convert these to excel's normal storage schema, I get the
> following:
>
> 42935.5625
> 42935.56944
>
> Just try to convert this to a POSIX class gives me issues.
>
> > dt <- c(42935.5625,42935.569444)
>
> > as.POSIXct(dt,origin="1899-12-30 00:00:00",tz="GMT")
>
> [1] "1899-12-30 11:55:36 GMT" "1899-12-30 11:55:36 GMT"
>
> As you can see, there is a world of difference here.  I've tried any
> number of solutions such as lubridate, etc and I get the same result
>
> > as_datetime(dt,origin="1899-12-30 00:00:00")
>
> [1] "1899-12-30 11:55:36 UTC" "1899-12-30 11:55:36 UTC"
>
> Any ideas about what I'm doing wrong?
>
>
> Shawn Way
>
> __
> 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] ls() pattern question

2021-07-14 Thread Andrew Simmons
Hello,


First, `ls` does not support `!=` for pattern, but it's actually throwing a
different error. For `rm`, the objects provided into `...` are substituted
(not evaluated), so you should really do something like

rm(list = ls(pattern = ...))

As for all except "con", "DB2", and "ora", I would try something like

setdiff(ls(), c("con", "DB2", "ora"))

and then add `rm` to that like

rm(list = setdiff(ls(), c("con", "DB2", "ora")))

On Wed, Jul 14, 2021 at 7:41 PM Kai Yang via R-help 
wrote:

> Hello List,
> I have many data frames in environment.  I need to keep 3 data frames
> only, con DB2 and ora.
> I write the script to do this.
> rm(ls(pattern != c("(con|DB2|ora)")))
>
>
> but it give me an error message:
>
>
> Error in rm(ls(pattern != c("(con|DB2|ora)"))) :
>   ... must contain names or character strings
>
> I think the pattern option doesn't support != ? and is it possible to fix
> this?
> Thank you,
> Kai
>
>
>
> [[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 for strsplit function

2021-07-07 Thread Andrew Simmons
Hello,


I would suggest something like `tools::file_path_sans_ext` instead of
`strsplit` to remove the file extension. This is also vectorized, so you
won't have to use a `sapply` or `vapply` on it. I hope this helps!

On Wed, Jul 7, 2021 at 9:28 PM Kai Yang via R-help 
wrote:

>  Hello List,
> I have a  one column data frame to store file name with extension. I want
> to create new column to keep file name only without extension.
> I tried to use strsplit("name1.csv", "\\.")[[1]] to do that, but it just
> retain the first row only and it is a vector.  how can do this for all of
> rows and put it into a new column?
> thank you,
> Kai
>
>
>
>
> [[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.


  1   2   >