[R] ODP: Modificatio of function body within a function

2022-04-03 Thread Grzegorz Smoliński
Thank you, Bert!

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


[R] Modificatio of function body within a function

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

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

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

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

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

Best regards,

Grzegorz

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


Re: [R] How to modify object's code living in some environment?

2021-12-29 Thread Grzegorz Smoliński
Thank you. I posted this question on SO in the meantime as well, but
there is no answer for my question, so I pasted your answer
(https://stackoverflow.com/questions/70517625/how-to-use-trace-for-function-in-parent-or-child-environment).
Hope it is fine for you.

śr., 29 gru 2021 o 11:24 Duncan Murdoch  napisał(a):
>
> On 28/12/2021 4:21 p.m., Grzegorz Smoliński wrote:
> > Thank you for all the comments. If this is not a problem, I would like
> > to continue this thread as for me a lot of questions are still open.
> > But if I should post other questions in different messages / topics,
> > please let me know.
> >
> > I didn’t answer Ivan’s question previously, but yes, my desired result
> > is indeed what debug() / debugonce() does and Ivan mentioned trace()
> > earlier also, so I have tried this. I thought that trace() will be
> > better in my case (than debug()) since I would like to be able to call
> > trace() from one environment and insert browser() (using trace()) to
> > the function defined in parent environment (still not sure if I
> > understand this concept of environments).
> >
> > Even if I would like to use it in an shiny app, I have started with
> > this example:
> >
> > --
> >
> > fun1 <- function() {
> >a <-  1
> >a
> > }
> >
> > fun2 <- function() {
> >fun1 <- function(){
> >  b <- 2
> >  b
> >}
> >trace(fun1, browser, where = globalenv())
> > }
> >
> > fun1()
> > fun2()
> > fun1()
> >
> > 
> >
> > So I was hoping to use trace inside child environment of global env,
> > i.e. set browser() using trace() to the body of function defined in
> > parent environment, but for sure I do not understand this since
> > trace() in my example above is putting the browser() function inside
> > fun1() function defined not in global environment, but in the body of
> > fun2(). I.e. I'm starting to browse the fun1() function with the local
> > variable 'b', not 'a' as I wanted.
>
> You are being bitten by normal R scoping.  While fun2 is executing, as
> soon as it executes the fun1 assignment the name fun1 is bound to the
> local function, so when you use fun1 in the first argument to trace(),
> you get that one.  It will ignore then "where" argument because it was
> passed a function object.  If you quote the name it will have to do a
> lookup and it will use "where".  So write fun2 like this:
>
> fun2 <- function() {
>fun1 <- function(){
>  b <- 2
>  b
>}
>trace("fun1", browser, where = globalenv())
> }
>
>
> >
> > Can I ask for help with this? In other words, how can I refer to some
> > function from the parent environment (not necessarily defined in the
> > global environment) being in the child environment?
>
> You could have used parent.frame() in place of globalenv() if you want a
> function that was visible to the caller of fun2.  If you want a function
> that was defined with the same environment as fun2, you can usually use
> environment(fun2), or parent.env(environment()).
>
>
> >
> > Also, if I could ask - is the reversed operation possible? I mean -
> > can I refer to some function from the child environment being in the
> > parent environment? I guess not, because of ephemerality?
>
> Sometimes you can, but not usually.  If fun2 had returned a value that
> referenced the evaluation frame, you would be able to see the "b"
> version of fun1 there.  But yours didn't.  This one would:
>
>
> fun2 <- function() {
>fun1 <- function(){
>  b <- 2
>  b
>}
>trace("fun1", browser, where = globalenv())
>environment()
> }
>
> Now if you do e <- fun2(), you'll set the trace on the global fun1, but
> e$fun1 will be the local one.
>
> I don't really know how Shiny sets things up, so I can't help with the
> stuff below.
>
> Duncan Murdoch
>
>
> >
> > As I mentioned, I'm thinking about all of this in the context of shiny
> > app and the shiny app which consists of multiple files, modules. I'm
> > thinking if I could refer to any function from any environment (or at
> > least to any function in any parent environment) from one place (one,
> > current environment) where I will call debug() or trace() to insert
> > browser() at the beginning of this chosen function. And by "any
> > function" I'm thinking about 

Re: [R] How to modify object's code living in some environment?

2021-12-28 Thread Grzegorz Smoliński
Thank you for all the comments. If this is not a problem, I would like
to continue this thread as for me a lot of questions are still open.
But if I should post other questions in different messages / topics,
please let me know.

I didn’t answer Ivan’s question previously, but yes, my desired result
is indeed what debug() / debugonce() does and Ivan mentioned trace()
earlier also, so I have tried this. I thought that trace() will be
better in my case (than debug()) since I would like to be able to call
trace() from one environment and insert browser() (using trace()) to
the function defined in parent environment (still not sure if I
understand this concept of environments).

Even if I would like to use it in an shiny app, I have started with
this example:

--

fun1 <- function() {
  a <-  1
  a
}

fun2 <- function() {
  fun1 <- function(){
b <- 2
b
  }
  trace(fun1, browser, where = globalenv())
}

fun1()
fun2()
fun1()



So I was hoping to use trace inside child environment of global env,
i.e. set browser() using trace() to the body of function defined in
parent environment, but for sure I do not understand this since
trace() in my example above is putting the browser() function inside
fun1() function defined not in global environment, but in the body of
fun2(). I.e. I'm starting to browse the fun1() function with the local
variable 'b', not 'a' as I wanted.

Can I ask for help with this? In other words, how can I refer to some
function from the parent environment (not necessarily defined in the
global environment) being in the child environment?

Also, if I could ask - is the reversed operation possible? I mean -
can I refer to some function from the child environment being in the
parent environment? I guess not, because of ephemerality?

As I mentioned, I'm thinking about all of this in the context of shiny
app and the shiny app which consists of multiple files, modules. I'm
thinking if I could refer to any function from any environment (or at
least to any function in any parent environment) from one place (one,
current environment) where I will call debug() or trace() to insert
browser() at the beginning of this chosen function. And by "any
function" I'm thinking about another thing problematic for me. If I
consider this example:

---
mod.R inside R/
---

mod_UI <- function(id) {
}

name_mod <- function(input, output, session) {
  fun1 <- reactive({
a  <-  1
  })
}

---
app.R
---

library(shiny)

ui <- fluidPage(
  mod_UI("mod"),
  textOutput("env")
)

server <- function(input, output, session) {

  name_mod("mod")

  output$env <- renderPrint({
names(environment(name_mod))
  })

  observe({
#trace(fun1, browser, where = environment(name_mod))
  })
}

shinyApp(ui, server)
---
I tried to refer to the fun1 in environment(name_mod) - if I'm not
wrong, this is my parent environment, where objects "mod_UI" and
"name_mod" exist, but obviously I'm doing this wrong, right? Because
here: "#trace(fun1, browser, where = environment(name_mod))" I have
tried to refer to the module environment, but I should refer to the
environment inside "name_mod". I don't know how to do this and here I
also would like to ask for help.

I know this may not be the best place for questions regarding shiny,
but I think these questions are purely linked to environment topics.

I also remember Bert's post about "body()" and yes, this seems to be a
better idea than my first try in my first post, but "trace()" and
"debug()" seem much easier as all I want is to insert browser(). But I
just can't get it, how to refer to functions in (any) other
environments.

Thank you very much for all your help and I hope it is OK to keep asking :).

pon., 27 gru 2021 o 18:28 Duncan Murdoch  napisał(a):
>
> On 27/12/2021 8:25 a.m., Duncan Murdoch wrote:
> > On 27/12/2021 8:06 a.m., Grzegorz Smoliński wrote:
> >> Hi,
> >>
> >> I know it is possible to find the environment in which some object
> >> lives using the 'environment()' function and the name of this object,
> >> but how to modify code of this object after this? Below is MRE:
> >
> > You are misunderstanding the relation between environments and
> > functions.  However, your understanding is also being messed up by a bug
> > in R, so it's not entirely your fault.
>
> Actually this isn't a bug in R, it is working as documented.  For a
> detailed explanation, see the response to my bug report here:
> https://bugs.r-project.org/show_bug.cgi?id=18269 .
>
> For a quick idea:  "complex assignments" are assignments where th

Re: [R] How to modify object's code living in some environment?

2021-12-27 Thread Grzegorz Smoliński
Thank you, Ivan and Duncan. The truth is I still feel lost in this
concept of environments, generally speaking.

What I really thought about is the possibility to make a shiny app
where there will be possible to choose the function name (from the set
of all functions defined in this app / modules / other helper files
sourced on the start of app) and write on the beginning of the body of
this chosen function 'browser()'. I have developed an app using
modules and I have found that it is not convenient to stop the app,
write 'browser()' where needed, start the app and make something to
run the function where is `browser()`, so I have started to think if I
could write this `browser()` on demand, using the app itself (so:
without stopping the app), then make something to run the function,
where is 'browser()' and enabling interactive debugger. But I see now
it is not good to make too many assumptions didn't knowing some topic
:)

pon., 27 gru 2021 o 14:27 Duncan Murdoch  napisał(a):
>
> On 27/12/2021 8:25 a.m., Duncan Murdoch wrote:
> > On 27/12/2021 8:06 a.m., Grzegorz Smoliński wrote:
> >> Hi,
> >>
> >> I know it is possible to find the environment in which some object
> >> lives using the 'environment()' function and the name of this object,
> >> but how to modify code of this object after this? Below is MRE:
> >
> > You are misunderstanding the relation between environments and
> > functions.  However, your understanding is also being messed up by a bug
> > in R, so it's not entirely your fault.
> >
> > More details below:
> >>
> >> test <- function() 1
> >>
> >> test() # 1
> >>
> >> environment(test)$test <- eval(parse(text = "function() 2"))
> >
> > First:  while it's usually true that function f will be found in
> > environment(f), these are really separate concepts.
> >
> > The environment of a function is where it will start looking for
> > non-local variables. It is initially the environment in which the
> > function definition is evaluated.
> >
> > The environment holding a function can be completely different.  This
> > most commonly shows up when you have functions that create other
> > functions.  For example,
> >
> > factory <- function(x) {
> >   force(x)
> >   function() print(x)
> > }
> >
> > f <- factory(3)
> > f()
> >
> > which will print 3.  Here environment(f) is the evaluation frame of the
> > call to factory(3), while f is not stored there, it is stored in the
> > global environment.  (If you run ls(environment(f)) you won't see "f"
> > listed, only "x".)
> >
> > In your first line "test <- function() 1" you create a function named
> > test in the global environment whose environment is also the global
> > environment.
> >
> > The third line should do what you want, i.e. put the new definition into
> > the environment of the test function, using the name test, but it
> > doesn't:  I think that's a bug in R.  It should be equivalent to
> >
> > .GlobalEnv$test <- eval(parse(text = "function() 2"))
> >
> > or
> >
> > test <- eval(parse(text = "function() 2"))
> >
> > but as you saw, it's not.  They do what you were expecting your third
> > line to do.
> >
> >>
> >> test() # still 1
> >>
> >> .GlobalEnv$test <- eval(parse(text = "function() 3"))
> >>
> >> test() # 3
> >>
> >> The context is I have shiny app which code I would like to modify (add
> >> something) and the only way to refer to this environment I know is to
> >> use 'environment()' (as I see, those functions do not live in
> >> .GlobalEnv), but as you can see above, I can't use it to modify the
> >> code.
> >
> > You can, as long as you work around the bug in R.  Just change that
> > third line into two statements:
> >
> > e <- environment(test)
> > e$test <- eval(parse(text = "function() 2"))
> >
>
> And I forgot to mention, as Ivan did, that this might not do what you
> want, because of the difference in the concepts of "environment(f)" and
> "environment where f is found".
>
> 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.


[R] How to modify object's code living in some environment?

2021-12-27 Thread Grzegorz Smoliński
Hi,

I know it is possible to find the environment in which some object
lives using the 'environment()' function and the name of this object,
but how to modify code of this object after this? Below is MRE:

test <- function() 1

test() # 1

environment(test)$test <- eval(parse(text = "function() 2"))

test() # still 1

.GlobalEnv$test <- eval(parse(text = "function() 3"))

test() # 3

The context is I have shiny app which code I would like to modify (add
something) and the only way to refer to this environment I know is to
use 'environment()' (as I see, those functions do not live in
.GlobalEnv), but as you can see above, I can't use it to modify the
code.

Best regards,

Grzegorz Smoliński

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] Wild cards for dataframes

2021-10-22 Thread Grzegorz Smoliński
Well, Wikipedia is probably the place where people who know some topic
can check if people who wrote the article did it right :)

You can try this short subchapter in R for Data Science (by Hadley
Wickham) as a starting point:

https://r4ds.had.co.nz/strings.html#matching-patterns-with-regular-expressions

Best regards,
Grzegorz


pt., 22 paź 2021 o 15:14 Eric Berger  napisał(a):
>
> You can check out Wikipedia for regular expressions:
>
> https://en.wikipedia.org/wiki/Regular_expression
>
>
>
>
> On Fri, Oct 22, 2021 at 3:48 PM Steven Yen  wrote:
>
> > Thanks, it works!
> >
> > What can I read to understand more about this part "\\..*$" of the
> > pattern? And more such as ^ and $ that I know from experience?
> >
> > On 2021/10/22 下午 06:22, Rui Barradas wrote:
> > > Hello,
> > >
> > > Use ls() with argument pattern. It accepts a regex and returns a
> > > vector of objects names matching the pattern.
> > >
> > >
> > > rm(list = ls(pattern = "data\\..*$"))
> > >
> > >
> > > Hope this helps,
> > >
> > > Rui Barradas
> > >
> > > Às 10:20 de 22/10/21, Steven Yen escreveu:
> > >> I like to be able to use a command with something similar to a "wild
> > >> card". Below, lines 4 works to delete all three dataframes, but line
> > >> 5 does not work. Any elegant way to accomplish this? My list of
> > >> dataframes can be long and so this would be convenient.
> > >>
> > >> data.1<-data.frame(x=1:3,y=4:6,z=7:9)
> > >> data.2<-data.1
> > >> data.3<-data.1
> > >> rm(data.1,data.2,data.3)
> > >> rm(data.*)
> > >>
> > >> __
> > >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >> https://stat.ethz.ch/mailman/listinfo/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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] creating a new variable and merging it on the dataframe

2021-10-18 Thread Grzegorz Smoliński
Hi,

If you had really just used:

wbpractice %>%
mutate(gap = total.food.exp-total.nfood.exp)  #gen a variable

and then checked by:

names(wbpractice)

then the problem is just with missed assignment, i.e. it should be:

wbpractice <- wbpractice %>%
mutate(gap = total.food.exp-total.nfood.exp)  #gen a variable

Best regards,
Grzegorz

pon., 18 paź 2021 o 13:37 PIKAL Petr  napisał(a):
>
> Hi
>
> I cannot say anything about mutate but
>
> read.csv results in data frame
>
> you can use then
>
> wbpractice$gap <- with(wbpractice, total.food.exp-total.nfood.exp)
>
> Cheers
> Petr
>
> BTW, do not use HTML formating your email is a mess.
>
>
> > -Original Message-
> > From: R-help  On Behalf Of Admire Tarisirayi
> > Chirume
> > Sent: Monday, October 18, 2021 1:26 PM
> > To: Jim Lemon 
> > Cc: r-help mailing list 
> > Subject: [R] creating a new variable and merging it on the dataframe
> >
> > Good day colleagues. Below is a csv file attached which i am using in my
> > analysis.
> >
> >
> >
> > hh.id
> >
> > hd17.perm
> >
> > hd17employ
> >
> > health.exp
> >
> > total.food.exp
> >
> > total.nfood.exp
> >
> > 1
> >
> > 2
> >
> > yes
> >
> > 1654
> >
> > 23654
> >
> > 23655
> >
> > 2
> >
> > 2
> >
> > yes
> >
> > 2564
> >
> > 265897
> >
> > 65984
> >
> > 3
> >
> > 6
> >
> > no
> >
> > 2547
> >
> > 123311
> >
> > 52416
> >
> > 4
> >
> > 8
> >
> > no
> >
> > 5698
> >
> > 13648
> >
> > 12544
> >
> > 5
> >
> > 6
> >
> > no
> >
> > 1254
> >
> > 36549
> >
> > 12365
> >
> > 6
> >
> > 8
> >
> > yes
> >
> > 1236
> >
> > 236541
> >
> > 26522
> >
> > 7
> >
> > 8
> >
> > no
> >
> > 4521
> >
> > 13264
> >
> > 23698
> >
> >
> >
> >
> >
> > So I created a df using the above csv file as follows
> >
> > wbpractice <- read.csv("world_practice.csv")
> >
> > Now, I wanted to create a new variable called gap and scripted and
> executed
> > the following command :
> >
> > wbpractice %>%
> >
> > mutate(gap = total.food.exp-total.nfood.exp)  #gen a variable
> >
> >
> >
> > By recalling  wbpractice, I could not see the new variable created.
> Running
> > the command;
> >
> > names(wbpractice)
> >
> >
> >
> > shows the old variables only. Any help on how to append the newly created
> > variable on my data?
> >
> >
> > Alternative email: addtar...@icloud.com/tchir...@rbz.co.zw
> > Skype: admirechirume
> > Call: +263773369884
> > whatsapp: +818099861504
> >
> >
> >
> > >
> >
> >   [[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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] [EXTERNAL] Re: unexpected behavior in apply

2021-10-08 Thread Grzegorz Smoliński
This will work as well:

d<-data.frame(d1 = letters[1:3],
  d2 = c(1,2,3),
  d3 = c(NA_character_,NA_character_,6))

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

d1  d2  d3
FALSE  TRUE FALSE

i.e. when NA changed do NA_character_

pt., 8 paź 2021 o 20:44 Derickson, Ryan, VHA NCOD via R-help
 napisał(a):
>
> This is interesting and does seem suboptimal. Especially because if I start 
> with a matrix from the beginning, it behaves as expected.
>
> > d<-data.frame(d1 = letters[1:3],
> +   d2 = c("1","2","3"),
> +   d3 = c(NA,NA,"6"))
> >
> > str(d)
> 'data.frame':   3 obs. of  3 variables:
>  $ d1: chr  "a" "b" "c"
>  $ d2: chr  "1" "2" "3"
>  $ d3: chr  NA NA "6"
> >
> > apply(d, 2, FUN=function(x)all(x[!is.na(x)] <= 3))
>d1d2d3
> FALSE  TRUE FALSE
>
>
>
>
> -Original Message-
> From: Jiefei Wang 
> Sent: Friday, October 8, 2021 2:22 PM
> To: Derickson, Ryan, VHA NCOD 
> Cc: r-help@r-project.org
> Subject: [EXTERNAL] Re: [R] unexpected behavior in apply
>
> Ok, it turns out that this is documented, even though it looks surprising.
>
> First of all, the apply function will try to convert any object with the dim 
> attribute to a matrix(my intuition agrees with you that there should be no 
> conversion), so the first step of the apply function is
>
> > as.matrix.data.frame(d)
>  d1  d2  d3
> [1,] "a" "1" NA
> [2,] "b" "2" NA
> [3,] "c" "3" " 6"
>
> Since the data frame `d` is a mixture of character and non-character values, 
> the non-character value will be converted to the character using the function 
> `format`. However, the problem is that the NA value will also be formatted to 
> the character
>
> > format(c(NA, 6))
> [1] "NA" " 6"
>
> That's where the space comes from. It is purely for making the result 
> pretty... The character NA will be removed later, but the space is not 
> stripped. I would say this is not a good design, and it might be worth not 
> including the NA value in the format function. At the current stage, I will 
> suggest using the function `lapply` to do what you want.
>
> > lapply(d, FUN=function(x)all(x[!is.na(x)] <= 3))
> $d1
> [1] FALSE
> $d2
> [1] TRUE
> $d3
> [1] FALSE
>
> Everything should work as you expect.
>
> Best,
> Jiefei
>
> On Sat, Oct 9, 2021 at 2:03 AM Jiefei Wang  wrote:
> >
> > Hi,
> >
> > I guess this can tell you what happens behind the scene
> >
> >
> > > d<-data.frame(d1 = letters[1:3],
> > +   d2 = c(1,2,3),
> > +   d3 = c(NA,NA,6))
> > > apply(d, 2, FUN=function(x)x)
> >  d1  d2  d3
> > [1,] "a" "1" NA
> > [2,] "b" "2" NA
> > [3,] "c" "3" " 6"
> > > "a"<=3
> > [1] FALSE
> > > "2"<=3
> > [1] TRUE
> > > "6"<=3
> > [1] FALSE
> >
> > Note that there is an additional space in the character value " 6",
> > that's why your comparison fails. I do not understand why but this
> > might be a bug in R
> >
> > Best,
> > Jiefei
> >
> > On Sat, Oct 9, 2021 at 1:49 AM Derickson, Ryan, VHA NCOD via R-help
> >  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://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fst
> > > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7C%7Cd4c50
> > > d8f8da547cbf36108d98a0c%7Ce95f1b23abaf45ee82

[R] ODP: unexpected behavior in apply

2021-10-08 Thread Grzegorz Smoliński
Hi,
but why is there a space before 6? Isn't it the source of the problem?

Best regards,
Grzegorz


pt., 8 paź 2021 o 20:14 Andrew Simmons  napisał(a):
>
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] ODP: ggplot question

2021-09-15 Thread Grzegorz Smoliński
Hi,

of course you can. This should work:

ggplot(s8_plot, aes(fill=GTresult, y=cases, x=gc_label)) +
geom_bar(position="stack", stat="identity")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))

By "hjust" you make sure that labels do not overlap on plot.

Best regards,

Grzegorz

śr., 15 wrz 2021 o 21:03 Kai Yang  napisał(a):
>
> Hi Grzegorz,
>
> You are correct. it works now.
>
> One more question: can I turn gc_label 90 degree in plot?
>
> Thank you
>
> Kai
>
> On Wednesday, September 15, 2021, 10:54:52 AM PDT, Grzegorz Smoliński 
>  wrote:
>
>
> Hi,
>
> Isn’t a bracket missing after gc_label?
>
> So it should be:
>
> > ggplot(s8_plot, aes(fill=GTresult, y=cases, x=gc_label)) +
>
> +  geom_bar(position="stack", stat="identity"))
>
> Best,
>
> Grzegorz
>
> Od: Kai Yang via R-help
> Wysłano: środa, 15 września 2021 19:50
> Do: R-help Mailing List
> Temat: [R] ggplot question
>
>
>
> Hello List,
>
> I use ggplot to draw a stack bar chart. but I error message. please
> look it below:
>
>
>
> > ggplot(s8_plot, aes(fill=GTresult, y=cases, x=gc_label) +
>
> +  geom_bar(position="stack", stat="identity"))
>
>
>
> Error: Mapping should be created with `aes()` or `aes_()`.
>
>
>
> GTresult and gc_label are character variables, cases is numeric
> variable. How to fix the problem?
>
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] ODP: ggplot question

2021-09-15 Thread Grzegorz Smoliński
Hi,

Isn’t a bracket missing after gc_label?

So it should be:

> ggplot(s8_plot, aes(fill=GTresult, y=cases, x=gc_label)) +

+   geom_bar(position="stack", stat="identity"))

Best,

Grzegorz

Od: Kai Yang via R-help
Wysłano: środa, 15 września 2021 19:50
Do: R-help Mailing List
Temat: [R] ggplot question



Hello List,

I use ggplot to draw a stack bar chart. but I error message. please
look it below:



> ggplot(s8_plot, aes(fill=GTresult, y=cases, x=gc_label) +

+   geom_bar(position="stack", stat="identity"))



Error: Mapping should be created with `aes()` or `aes_()`.



GTresult and gc_label are character variables, cases is numeric
variable. How to fix the problem?

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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] [R-pkgs] dedupewider - package to dedupe records across multiple columns

2021-07-16 Thread Grzegorz Smoliński
Dear All,

I would like to announce a one-function package dedupewider. I have
used it to find companies connected by some telephone numbers to
collapse it into one row, e.g. in case of this table:

|tel_1|tel_2|name|

|||nam1|
|||nam2|
|||nam3|

all records will be collapsed into one record with 4 unique telephone
numbers (optionally all unique names or information from other columns
can be kept). Perhaps it will be helpful for you as well.

Link to CRAN: https://CRAN.R-project.org/package=dedupewider

Best regards,

Grzegorz Smoliński

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

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