[R] how to find the table in R studio

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Wed Jan 12 22:46:37 CET 2022


If you intend to use R for work beyond simple scripts and plots, you will
have to understand how to use functions in R: the principle that Duncan
mentioned -- that objects such as f-table that are local to a function are
not available outside the function (with caveats -- see below) is
__absolutely fundamental__ to function use in R. You can search for a
suitable R function tutorial on the web of course -- there are tons.

As you learn about how to use functions in R, you will encounter the
concepts of __scoping__ in R and R __environments__. Unfortunately, this
does get a bit technical, but it is very useful to understand at least the
basics for working with functions; and it explains the below example, which
is the 'caveat' I referred to. Feel free to ignore, of course, if you think
this is too much at this point.

*********  R function scoping/environment example ********

## create a function that returns a function as its output
 outer_fun<- function(x){
    inner_fun <- function(y)x*y  ## the function definition, a one-liner
        ## the function returns x*y with input y
   inner_fun ## the last object in a function is automatically returned
      ## so here a function is returned
 }

## let's see
> infun10 <- outer_fun(10)   ## x = 10
## note that the function returned by outer_fun(10) was assigned to infun10

> x  ## but x is not present in the global environment/command line window
Error: object 'x' not found

> infun10(5) ## but infun10 somehow 'knows' about x
[1] 50 ## = 10*5

> rm(outer_fun) ## what if we remove outer_fun completely
> outer_fun
Error: object 'outer_fun' not found  ## yup, it's gone

> infun10(2)
[1] 20  ## But infun10 still knows about x

## this is because x is in the **environment** of infun10
> environment(infun10)$x
[1] 10
## and it can even be changed there:
> environment(infun10)$x <- 3
> infun10(2)
[1] 6 ## now it's 3*2 instead of 10*2

So there's obviously a lot going on under the hood here. If you decide that
you want (or need) to get into at least some of this, please note that this
list cannot serve as a tutorial service. We can help, as you know, but we
expect that you will put in the effort required. As I said, there are a ton
of (good) tutorials out there to choose from.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Jan 12, 2022 at 12:07 PM Kai Yang via R-help <r-help using r-project.org>
wrote:

> Hi all,
> I created a function in R. It will be generate a table "temp". I can view
> it in R studio, but I cannot find it on the top right window in R studio.
> Can someone tell me how to find it in there? Same thing for f_table.
> Thank you,
> Kai
> library(tidyverse)
>
> f1 <- function(indata , subgrp1){
>   subgrp1 <- enquo(subgrp1)
>   indata0 <- indata
>   temp    <- indata0 %>% select(!!subgrp1) %>% arrange(!!subgrp1) %>%
>     group_by(!!subgrp1) %>%
>     mutate(numbering =row_number(), max=max(numbering))
>   view(temp)
>   f_table <- table(temp$Species)
>   view(f_table)
> }
>
> f1(iris, Species)
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/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]]



More information about the R-help mailing list